Dismissing iPad UIPopoverController when BarButtonItem is pushed while it's open

后端 未结 7 1210
闹比i
闹比i 2020-12-14 11:36

Using a split view on the iPad, I have the following code:

- (void) splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController         


        
7条回答
  •  悲哀的现实
    2020-12-14 12:18

    This is a lot easier because the popoverController is a property. Makes it easier to reference.

    if ([self.popoverController isPopoverVisible]) {
        //using the setters and getters "goes thru the proper channels" when accessing objects
        [self.popoverController dismissPopoverAnimated:YES];
    } else {
        UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:YOUR_VIEW_CONTROLLER];
        self.popoverController = pc;
        [pc release];
    
        //get the button instance you set on the toolbar
        UIBarButtonItem *categoryButton = [[toolbar items] objectAtIndex:0];
        [self.popoverController presentPopoverFromBarButtonItem:categoryButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
    

    I actually just realized that you're referring to the code inside the Delegate method for displaying the viewController at index:0 of your splitView. This answer doesn't necessarily apply to that, but does apply to any other time you're accessing and creating popoverControllers on the iPad. Without checking if a popover is visible first, you will either crash, or open several popovers.

    Thanks for your time.

提交回复
热议问题