ARC and Popovers and delegates-Collection of common programming errors
i am tearing my hair out, I have migrated my old project to arc and I’m getting this error popping up : * Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘-[UIPopoverController dealloc] reached while popover is still visible.’
I have read through some threads and I’m confused, some say when using delegates use a weak reference but on the other hand when using popovers use a strong property reference, can someone give me an example of how best to use ARC and delegates with a popover that has a button inside that changes the background colour for example?
From what I’ve read I keep hearing use an instance variable in my view controller, here it is in my main view controller:
@property (nonatomic, strong) UIPopoverController *currentPopover;
and the is the method implementation in the main view controller file:
- (IBAction)ShowPopTextColor:(id)sender {
if (currentPopover == nil) {
TimerTextColor *timerTextColor = [[TimerTextColor alloc]init];
timerTextColor.delegate =self;
UIPopoverController *pop = [[UIPopoverController alloc]initWithContentViewController:timerTextColor];
[pop setDelegate:self];
[pop setPopoverContentSize:CGSizeMake(320, 240)];
[pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//[pop release];
} else {
[currentPopover dismissPopoverAnimated:YES];
currentPopover = nil;
}
}
here is my popup content header:
@protocol colorChooserDelegate
-(void) colorSelected:(UIColor*)thecolor;
@end
@interface TimerTextColor : UIViewController{
id delegate;
IBOutlet UIButton *colorView;
}
- (IBAction)buttonTapped:(id) sender;
@property (nonatomic,strong) iddelegate;
@property (nonatomic,strong) UIButton *colorView;
@end
What am i doing wrong?
-
Assign currentPopover. Call
currentPopover = pop
after popover creation
-
you shouldn’t create a local variable to store the popover controller.
Change this
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:timerTextColor];
to
self.currentPopover = [[UIPopoverController alloc] initWithContentViewController:timerTextColor];
Originally posted 2013-11-15 09:09:34.