ios7 NSInvalidArgumentException presenting UIPopoverController
Trying to show a UIPopoverController crashes in ios 7 while it works in ios6 and before. The error message is
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController _commonPresentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
In fact, it's true: self.view.window is nil in ios7 but not in ios6.
The code:
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:aViewCtl];
pop.delegate = self;
[pop presentPopoverFromRect:CGRectMake(aRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
The view is created (and assigned to self.view) programatically in the loadView method, while the popover is shown after pressing a button. Any suggestion?
EDIT: looks like the problem is using a modal view before showing the popup, in spite of dismissing it. Somehow, the self.view.window is lost after presenting it.
The best solution I've found is to check self.view.window. If it is nil, then register a delegate in the view and wait for didMoveToWindow
, which will call the delegate and proceed to create and show de popover.