ipadios5uisplitviewcontrollerportraitios5.1

UISplitViewController: How force to show master popover in app launch? (portrait)


In an iPad App i'm using the UISplitViewController. I need to force to show the master popover when the app launch in portrait mode.

Now I'm using this code and it works well on iOS 5.0.

if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
   if ([[[AppDelegate sharedAppDelegate] splitViewController] respondsToSelector:[[[AppDelegate sharedAppDelegate] btnMenu] action]]) {
      [[[AppDelegate sharedAppDelegate] splitViewController] performSelector:[[[AppDelegate sharedAppDelegate] btnMenu] action]];
   }            
}

But in iOS 5.1 (with the new type of master popover) the behaviour seems to be random. Sometimes the popover shows in fullscreen and sometimes works well.

Some suggestion for 5.1?


Solution

  • I struggled with this one for a while, and even now I'm not 100% happy with the solution, but it is the only thing I've been able to come up with, given the current constraints.

    First, override the following delegate method:

    - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
    

    and use it to grab a reference to the bar button item, and store it in an iVar:

    barButtonForMaster = barButtonItem;
    

    Then, when you want to show the master view controller, make a call like this:

    [barButtonForMaster.target performSelector: barButtonForMaster.action withObject: barButtonForMaster];
    

    In case you want to perform this right at the start, then use some delay in order to prevent app crashing (thanks to the helpful comment):

    [barButtonForMaster.target performSelector: barButtonForMaster.action withObject: barButtonForMaster afterDelay:1];
    

    In that case you can perform the selector right in the split view delegate method.