I am getting the following crash when I open a popover by tapping on a bar button item on my main controller and then rotating the view.
"NSGenericException: UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs."
I looked at a similar crash here - UIActivityViewController crashing on iOS8 iPads, but I am not directly using "UIPopoverPresentationController" anywhere in my code.
My main controller is an UIViewController and has a barbuttonitem which when tapped opens up a popover as another controller which is UITableViewController (DisplayTableViewController class). Below is the code:
In my main controller in a custom method say "createPopover" I have:
- (void)createPopover
{
self.tableViewController = [[DisplayTableViewController alloc] init];
self.tableViewController.someDelegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.tableViewController];
// Display the popover.
self.navBarButtonItemPopoverController = [[UIPopoverController alloc] initWithContentViewController:navigationController];
self.navBarButtonItemPopoverController.delegate = self;
[self displayPopover:self.myBarButton];
}
- (void)displayPopover:(id)sender
{
[self.navBarButtonItemPopoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
When I rotate the view this gets executed. This code is also in my main controller:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
if ([self.navBarButtonItemPopoverController isPopoverVisible])
{
[self.navBarButtonItemPopoverController dismissPopoverAnimated:NO];
[self performSelector:@selector(displayPopover:) withObject:self.myBarButton afterDelay:0.0];
}
}
After this the app crashes with the mentioned error. In my createPopover method, I tried:
self.tableViewController.popoverPresentationController.sourceView = [self view];
But nothing helps. When I print out "self.tableViewController.popoverPresentationController" or "self.popoverPresentationController" anytime in the life cycle, it's always nil. Also I noticed "sender" in displayPopover method is always present i.e. before rotation and after rotation too and is the same object.
Any idea why the popoverPresentationController is nil or why the app is crashing?