Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

objective c - UISplitViewController in portrait: how to hide master popover programmatically?

In my UISplitViewController the master controller is a UINavigationController. When in portrait mode I would like to keep the navigation controller visible as long as the user navigates upwards (using the back button). As soon as an item from the table view of the navigation controller is selected, I want to dismiss the popover. How can I achieve this? How can my UITableViewController know if it is inside a popover, and if yes, dismiss itself?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...