UISplitViewController: how to present a popover?

I have a UISplitViewController which is a UISplitViewControllerDelegate with the following delegate method:

splitViewController: willHideViewController: withBarButtonItem: forPopoverController:

When the iPad launches in Portrait, I would like the Popover from SplitView to be visible. How can i do this?

I tried the following code:

 - (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc { //setting the barButtonItem in the toolbar in the detail view. [pc presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO]; } 

But the code above gives me the following error:

The application terminated due to the uncaught exception "NSInvalidArgumentException", reason: '- [UIPopoverController presentPopoverFromRect: inView: allowedArrowDirections: animated:]: pop-ups cannot be presented from a view that does not have a window.

+4
source share
1 answer

there is only one problem, the wrong place to call the presentPopover method, splitViewController: * WillHide * ViewController ....... therefore, barButtonItem exists, but is not present on the screen. I used the following code and it worked for me. To handle all cases you need to use 2 methods.

 - (void)viewDidAppear:(BOOL)animated { if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) { if (self.view.window != nil) { [_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO]; } } [super viewDidAppear:animated]; } 

and

 -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { if (fromInterfaceOrientation == UIDeviceOrientationLandscapeLeft || fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) { if (self.view.window != nil) { [_masterPopoverController presentPopoverFromRect:CGRectMake(0, 0, 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO]; } } } 
+4
source

Source: https://habr.com/ru/post/1389920/


All Articles