UISplitViewController in portrait: how to hide master popover programmatically?

前端 未结 3 1749
执笔经年
执笔经年 2020-12-14 23:01

In my UISplitViewController the master controller is a UINavigationController. When in portrait mode I would like to keep the navigation controller

相关标签:
3条回答
  • 2020-12-14 23:11

    Make your main view controller a UISplitViewControllerDelegate (if it isn't already) and wire it up to the UISplitViewController's delegate outlet.

    Create a UIPopoverController variable in your main view controller:

    // MyViewController.h
    @interface MyViewController : UIViewController <UISplitViewControllerDelegate> {
        UIPopoverController *popoverController;
    }
    @property (retain, nonatomic) UIPopoverController *popoverController;
    
    // MyViewController.m
    @synthesize popoverController;
    

    Implement the following UISplitViewControllerDelegate methods:

    // Called when rotating to portrait
    - (void)splitViewController:(UISplitViewController*)svc
         willHideViewController:(UIViewController *)aViewController
              withBarButtonItem:(UIBarButtonItem*)barButtonItem
           forPopoverController:(UIPopoverController*)pc {
    
        // Popover controller is visible in portrait
        self.popoverController = pc;
    }
    
    // Called when rotating to landscape
    - (void)splitViewController:(UISplitViewController*)svc
         willShowViewController:(UIViewController *)aViewController
      invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    
        // No popover controller in landscape view
        self.popoverController = nil;
    }
    

    In your own handler in the main view controller (the one that gets called when a naviation item is selected in the table view):

    - (void)navigationControllerSelectedItem:(id)item {
        // If a popover controller is visible, hide it
        if (popoverController) {
            [popoverController dismissPopoverAnimated:YES];
        }
    }
    

    And don't forget to release that variable:

    - (void)dealloc {
        self.popoverController = nil;
        [super dealloc];
    }
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-14 23:15

    The IOS 6.0 SplitView template has this built in. The detail view tracks the orientation and the MasterViewController popover.

    Simply set the detailItem and the popover disappears if appropriate. There is even a check if you are using the same detaiItem so no page setup and refresh work gets done.

    self.detailViewController.detailItem = self.detailViewController.detailItem;
    
    0 讨论(0)
  • 2020-12-14 23:34

    The standard iPad sample for SplitViewController in iOS5 does about the same as the elaborate answer, but the popoverController is called masterPopoverController.

    And creating the property iOS5 style as _popoverController does not work, because there as already an ivar with that name in UIViewController.h.

    0 讨论(0)
提交回复
热议问题