经过一些工作后,我有一个弹出视图,显示其内部的自定义uiview正常显示。 当你点击popover视图外面时,它会按预期解散,但是我也希望当它内部的按钮被点击时它会被忽略,有没有人可以帮我解决这个问题?苦苦寻找简明指南。
答案 0 :(得分:12)
要手动关闭弹出窗口,您需要保留对它的引用。一个好的地方就是在显示弹出窗口的视图控制器中。
要让内容视图控制器内的按钮告诉原始视图控制器(显示弹出窗口)忽略弹出框,可能的两种方法是委托+协议或NSNotificationCenter。
下面的示例代码使用NSNotificationCenter。
在显示popover(“PresenterViewController”)的视图控制器的.h中,为popoverViewController添加一个属性:
@interface PresenterViewController : UIViewController {
...
UIPopoverController *popoverController;
}
...
@property (nonatomic, retain) UIPopoverController *popoverController;
@end
在PresenterViewController的.m中:
@synthesize popoverController;
...
- (void)showPopover
{
if (self.popoverController == nil)
{
ContentFooViewController *content =
[[ContentFooViewController alloc] init];
UIPopoverController *popover = [[UIPopoverController alloc]
initWithContentViewController:content];
[content release];
self.popoverController = popover;
[popover release];
// Listen for the "done" notification which will be posted
// by the button in the content view controller.
// When the notification is received,
// call the contentFooViewControllerDone: method...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(contentFooViewControllerDone:)
name:@"contentFooViewControllerDone"
object:popoverController.contentViewController];
}
[self.popoverController presentPopoverFromRect:...
// OR
[self.popoverController presentPopoverFromBarButtonItem:...
}
- (void)contentFooViewControllerDone:(NSNotification *)notification
{
// Button in content view controller was tapped, dismiss popover...
[self.popoverController dismissPopoverAnimated:YES];
}
- (void)dealloc
{
//stop listening for notifications and release popoverController...
[[NSNotificationCenter defaultCenter] removeObserver:self];
[popoverController release];
[super dealloc];
}
在内容视图控制器中,按如下方式实现按钮点按方法:
- (IBAction)dismissButtonTapped
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"contentFooViewControllerDone" object:self];
}