www.rinner.st

the blog/wiki/website/homepage/internetpräsenz of Stefan Rinner

Monday, Mar 4, 2013

Ever wondered about an exception like this happening in a UIViewController which doesn't contain any UIPopover on an iPad?

Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.
…
…
___73-[UIPopoverController _completionBlockForDismissalWhenNotifyingDelegate:]_block_invoke_0 + 285

You're probably wondering what this crash is about and where this UIPopover is coming from - because all you do is showing some UIActionSheet.
To save you (and me as well) some more nights of debugging:
What happened in my case was that I displayed an UIActionSheet from the rightBarButtonItem of a UINavigationBar and then the user clicked the leftBarButtonItem which is a back button and dismisses the viewController - but not the UIActionSheet (on an iPad displayed as an UIPopover) which stays visible.
Then ARC deallocs the UIPopover which is still visible and therefore crashes your app. A solution to that is:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if(_shareActionSheet != nil) {
        [_shareActionSheet dismissWithClickedButtonIndex:-1
                                                animated:NO];
        _shareActionSheet = nil;
    }
}