Ios – UISplitViewController: How to present popover

cocoa-touchiosipadobjective cuisplitviewcontroller

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

splitViewController:willHideViewController:withBarButtonItem:forPopoverController:

When the iPad is started in Portrait, I would like the Popover from the SplitView to be visible. How can I do that?

I've 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 above code gives me the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'

Best Answer

there only one problem, wrong place to call presentPopover method, splitViewController:*WillHide*ViewController....... so, barButtonItem exist but not present on screen. i used next code and it worked for me. For handle all cases u 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];
        }
    }
}
Related Topic