Apple 的 HIG 表示,弹出框内不应有明确的关闭按钮,但要按照您的要求进行操作,您有两种选择。
1) 发布 NSNotification
或者
2)深入到您的视图层次结构,直到您有弹出实例
1)在 viewDidLoad 方法中,无论您在哪个视图中显示弹出框:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissThePopover) name:@"popoverShouldDismiss" object:nil];
创建一个名为“dismissThePopover”的方法,并在 dealloc 方法中,removeObserver
-(void)dismissThePopover {
[self.popoverController dismissPopoverAnimated:YES];
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
在你的 popoverController “dismiss” 按钮中,输入这一行:
[[NSNotificationCenter defaultCenter] postNotificationName:@"popoverShouldDismiss" object:nil];
这样做会向应用程序发送一个通知,并且由于您已经注册了另一个视图控制器来监听它,所以每当它看到该通知时,它都会调用您指定的选择器,在这种情况下,dismissThePopover。
2) 深入查看视图层次结构以找到 self.popoverController
看看这个,你的肯定会有所不同,但总体思路是一样的。从你的 AppDelegate 开始,进入第一个视图控制器,进入子视图,直到你到达你的 self.popoverController 对象。
MyAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
//appDelegate instance, in this case it's the .m file for your ApplicationDelegate
UISplitViewController *svc = appDelegate.splitViewController;
//In this case the first view inside the appDelegate is a SplitView, svc
UINavigationController *navc = [[svc viewControllers]objectAtIndex:0];
//a navigationController is at index:0 in the SplitView hierarchy. DetailView is at index:1
NSArray *vcs = [navc viewControllers];
//vcs is the array of different viewcontrollers inside the Navigation stack for nvc
iPadRootViewController *rootView = [vcs objectAtIndex:0];
//declare the rootView, which is the .m file that is at index:0 of the view array
UIPopoverController *pc = [rootView popoverController];
//HERE WE GO!!! popoverController is a property of iPadRootViewController's instance rootView, hereby referred to as pc.
[pc dismissPopoverAnimated:YES];
//bye bye, popoverController!
希望这可以帮助