I have a universal app that displays buttons inside an actionsheet.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:[dataObj.preferences objectForKey:@"appts_name"], @"Reserve", nil];
[actionSheet showInView:self.view];
On an iPad that automatically gets changed to a UIPopoverController. This displays and acts fine, however if I have the above popover being displayed and change orientation on the iPad then tap one of the buttons, it crashes in that buttons method where I am wanting to display another popover, saying that
-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.
If I do not rotate the iPad and just tap a button it executes the same code just fine.
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:addItemView];
popOver = [[UIPopoverController alloc] initWithContentViewController:navBar];
if (viewPopover) {
if (self.view.window != nil) {
CLS_LOG(@"Rect: %@ - View: %@", NSStringFromCGRect(rectPopover), viewPopover);
[popOver presentPopoverFromRect:rectPopover inView:viewPopover permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
} else {
// We should NEVER get here but just incase. I don't want the app to crash.
CLS_LOG(@"No viewPopover or rectPopover - %@ - %@", viewPopover, NSStringFromCGRect(rectPopover));
}
Log from the CLS_LOG
-[AppointmentViewController showAppointmentAdd] line 148 $ Rect: {{341.333, 592}, {1, 1}} - View: <UIScrollView: 0xc8c5d40; frame = (1044 0; 768 872); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xc8c7780>; contentOffset: {-0, 188}>
rectPopover and viewPopover are legit. Why is the popOver crashing saying it isn’t in a window, but only when I rotate the device?
EDIT:
I found out it is being caused because after orientation change, I am reloading the view to account for the new sizes. It seems this takes precedence in the window. It also only seems to affect the UIActionSheet.
Have you tried tried redisplaying the popover after rotation?
You know when the rotation is occurring (
willRotateToInterfaceOrientation:), and then you can redisplay the popover controller according to it’s new anchor point.