UIPopovercontroller dealloc reached while popover is still visible
Solution 1
UIPopoverControllers should always be held in an instance variable. It is a good practice to create a strong property for it.
UPDATE:
As of iOS 8 you should be using UIPopoverPresentationController
. Then you don't need to keep a reference to the popover because it is managed by the presentation controller.
Code example (works both on iPhone and iPad):
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
picker.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController* popoverPC = picker.popoverPresentationController;
popoverPC.barButtonItem = bbItem;
popoverPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:picker animated:YES completion:nil];
Solution 2
When the function exits there are no other reference to the popover controller, so it's deallocated too early.
Try adding it as a member of your class instead.
Tim
Solution 3
Adding what @phix23 answered, create *poc property like this:
@property (nonatomic, retain) IBOutlet UIPopoverController *poc;
and then change
UIPopoverController *poc = [[UIPopoverController alloc]
initWithContentViewController:picker];
for
self.poc = [[UIPopoverController alloc]
initWithContentViewController:picker];
Related videos on Youtube
![Mikayil Abdullayev](https://i.stack.imgur.com/P1vxW.jpg?s=256&g=1)
Mikayil Abdullayev
Updated on July 08, 2022Comments
-
Mikayil Abdullayev almost 2 years
I assure you that I did look for an answer in SO for my question but none of them were helpful. Here I got a simple code that should present a
UIImagePickerController
within aUIPopoverController
:-(void)takePicture:(id)sender{ UIImagePickerController *picker=[[UIImagePickerController alloc] init]; picker.delegate=self; picker.sourceType=UIImagePickerControllerSourceTypeCamera; picker.allowsEditing=YES; UIPopoverController *poc=[[UIPopoverController alloc] initWithContentViewController:picker]; [poc presentPopoverFromBarButtonItem:bbItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO]; }
Now, even from the first time I get
[UIPopoveController dealloc]
reached while... error and the program crashes. I'm not doing any retain,relase or autoreleases as per ARC. Is there any special consideration withUIPopoverControllers
when benefitting from ARC? -
Mikayil Abdullayev over 12 yearsOh, I see. But isn't this like a UIAlertView? I never have an ivar for it, I just alloc init it wherever I need, show it and then [used to] release. In what is popovercontroller different?
-
Mikayil Abdullayev over 12 yearsShouldn't I still be able to see it first before it gets deallocated?
-
fzwo over 12 years@Mikayil The alertView is retained by its superview (as all views are), but the popoverController isn't a view, so doesn't have a superview, so won't be retained by anybody if you don't retain it (or store it in a strong variable that has a scope that's longer than the current method - for instance an iVar).
-
Mikayil Abdullayev over 12 yearsBut I'm still confused about the retain count of the UIPopoverController. Because I put a check before I alloc and init one. And only if it's nil I alloc a new one. But after allocating it for the first time I never get it nil. I mean I call a method once. There I allocate and init my ivar. And the next time when I again call that method this time I find my ivar already allocated. If ARC takes care of this, then when does it release it. Or does it autorelease it?
-
Felix over 12 years@Mikayil ivars are released by ARC when the object is deallocated or when you set them to nil
-
Joshua Dance about 11 yearsYou don't have to put it in your .h file. That would make it public and unless you want that, just make it a property in your .m file.
-
Amit Battan over 10 yearsbut they have not mention this in the documentation, in How to Use section they use local variable