UISplitViewControllerDelegate

UISplitViewControllerDelegate

在我的UISplitViewController中,主 Controller 是UINavigationController
在纵向模式下,只要用户向上导航(使用“后退”按钮),我希望保持导航 Controller 可见。一旦从导航 Controller 的表格 View 中选择了一个项目,我想关闭弹出窗口。
我怎样才能做到这一点?我的UITableViewController如何知道它是否在弹出窗口内,如果是,则将其自身关闭?

最佳答案

将主 View Controller 设置为UISplitViewControllerDelegate(如果尚未安装),并将其连接到UISplitViewController的delegate socket 。

在主 View Controller 中创建一个UIPopoverController变量:

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

// MyViewController.m
@synthesize popoverController;

实现以下UISplitViewControllerDelegate方法:
// 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;
}

在主 View Controller 的自己处理程序中(在表 View 中选择导航项时调用的处理程序):
- (void)navigationControllerSelectedItem:(id)item {
    // If a popover controller is visible, hide it
    if (popoverController) {
        [popoverController dismissPopoverAnimated:YES];
    }
}

并且不要忘记释放该变量:
- (void)dealloc {
    self.popoverController = nil;
    [super dealloc];
}

希望对您有所帮助!

10-07 20:16