2

I'm stuck with an issue.... Please help!

I have a navigation controller project. From the root controller I am pushing a view onto the stack that has some info and 2 buttons (YES, NO). (it can't be an AlertView). If they press the YES button, I call a method on the parent view (using a delegate method) and pop the view from the stack. The method on the parent pushes a new view onto the stack. I assumed the parent method would remain in memory after the deallocation of the child view because the method exists on the parent, but when the child is deallocated it also deallocates everything created inside the method on the parent. Does anyone have a solution for responding to events that happen on a child view in a parent view that will remain after the child is deallocated?

Thanks!

in header file:

#import <UIKit/UIKit.h> 

@protocol decrementDelegate; 
@interface decrement : UIViewController { 
    int currentCount; 
    IBOutlet UILabel *countLabel; 
    id <decrementDelegate> delegate; 
} 
@property (nonatomic, assign) id <decrementDelegate> delegate; 
@end 

@protocol decrementDelegate <NSObject> 
-(void)decrementControllerDidFinish:(decrement *)controller 
        withString:(NSString *)stringValue; 
@end 

in implementation file:

[self.delegate decrementControllerDidFinish:self 
    withString:countLabel.text]; 
[self.navigationController popViewControllerAnimated:YES];

caller:

-(void)decrementControllerDidFinish:(decrement *)controller 
    withString:(NSString *)stringValue { 

    // Show our details  
    myNewChildController *viewController = [[myNewChildController alloc] 
        initWithNibName:@"myNewChildController" bundle:nil]; 
    viewController.delegate = self; 
    [self.navigationController pushViewController:viewController animated:YES]; 
    [viewController release]; 
}

Using non-delegate and just calling a method on the parent:

caller:

mySummary *parent = [self.navigationController.viewControllers objectAtIndex:0];
[mySummary setMyAllowOnParent:@"Allowed"]; 
[self.navigationController popViewControllerAnimated:YES]; 

parent:

-(void)setMyAllowOnParent:(NSString *)aAllow { 
newView *viewController = [[newView alloc] initWithNibName:@"newView" bundle:nil];  
[self.navigationController pushViewController:viewController animated:YES]; 
[viewController release]; 
}

newView crashes with deallocated error when caller view is deallocated. Does anyone have an idea how to resolve this issue?

vr6t
  • 41
  • 3

1 Answers1

0

How did you create your delegate? You didn't accidentally used retain instead of assign (assuming you use a property to access it)? Because if you used retain, this might have caused the issue.

By the way, you mention something about a UIAlertView that cannot be used, which leads me to think that you created a view with a similar design as a UIAlertView. Perhaps the following link might be useful:

how can i make popup login window similar as on istore in objective-C/cocoa-touch

Community
  • 1
  • 1
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • Wolgang, Thanks for the quick response. Here is my delegate @interface decrement : UIViewController { id delegate; } @property (nonatomic, assign) id delegate; -(void)decrementControllerDidFinish:(decrement *)controller withString:(NSString *)stringValue; I cannot use UIAlertView, as the UIView I create pushes it's own views onto the stack. I thought someone might suggest that – vr6t Apr 14 '11 at 17:16
  • Could you please show some more code? I guess you use an instance variable to keep track of the delegate, could you show me all relevant code to this ivar? **Edit:** seems you just edited it, seems the delegate is created correctly. Could you please add your code to your main question so i can see it formatted? I'd also like to see the code you use to pop the viewcontroller. – Wolfgang Schreurs Apr 14 '11 at 17:18
  • Wolfgang, Child header: #import @protocol decrementDelegate; @interface decrement : UIViewController { int currentCount; IBOutlet UILabel *countLabel; id delegate; } @property (nonatomic, assign) id delegate; @end @protocol decrementDelegate -(void)decrementControllerDidFinish:(decrement *)controller withString:(NSString *)stringValue; @end child m: [self.delegate decrementControllerDidFinish:self withString:countLabel.text]; [self.navigationController popViewControllerAnimated:YES]; – vr6t Apr 14 '11 at 17:26
  • parent: -(void)decrementControllerDidFinish:(decrement *)controller withString:(NSString *)stringValue { // Show our details myNewChildController *viewController = [[myNewChildController alloc] initWithNibName:@"myNewChildController" bundle:nil]; viewController.delegate = self; [self.navigationController pushViewController:viewController animated:YES]; [viewController release]; } – vr6t Apr 14 '11 at 17:26
  • myNewChildController is deallocated with the original child view – vr6t Apr 14 '11 at 17:28
  • Well, after the child view is released, the parent will never be able to respond any more, because the delegate property becomes nil. What is weird is that you claim that the viewController created in the delegate method on the caller is deallocated as the child viewController is deallocated. This is weird, but I can't see what's going wrong. Is the delegate method called from the main thread? – Wolfgang Schreurs Apr 14 '11 at 17:42
  • It's doing this in ALL of my views that call a method in a parent view. This must be a common issue? Everything created in a method on the parent view that is called from the child view is deallocated with the child view. – vr6t Apr 14 '11 at 17:46
  • Just to take this one step further... if I don't use a delegate. I just create a method on the parent view. and I call this on the child... mySummary *parent = [self.navigationController.viewControllers objectAtIndex:0]; [mySummary setMyAllowOnParent:@"Allowed"]; [self.navigationController popViewControllerAnimated:YES]; -(void)setMyAllowBanOnParent:(NSString *)aAllowBan { newView *viewController = [[newView alloc] initWithNibName:@"newView" bundle:nil]; [self.navigationController pushViewController:viewController animated:YES]; [viewController release]; } Same effect. how come? – vr6t Apr 14 '11 at 18:24
  • @vr6t - rather than comment, please edit your question, @wolfgang - I'm going to assume that you're just copying the OP's comments and will approve your edit – kdgregory Apr 14 '11 at 18:35
  • In the child or parent control, do you use -removeFromSuperview: somewhere perhaps? Additionally you could try settings the NS_ZombieEnabled environment variable to figure out if there are any zombie objects causing the crash, just make sure you remove this variable from any Release builds: http://stackoverflow.com/questions/1622079/break-on-exc-bad-access-in-xcode/1622091#1622091 – Wolfgang Schreurs Apr 14 '11 at 21:47