gpt4 book ai didi

cocoa-touch - 如何设置 Popover View 以正确关闭

转载 作者:行者123 更新时间:2023-12-04 10:44:34 25 4
gpt4 key购买 nike

经过一些工作,我得到了一个弹出 View ,其中显示了一个自定义 uiview,它正确显示。
当您在弹出 View 之外点击时,它会按预期关闭,但是我也希望它在点击其中的按钮时关闭,有没有人可以帮助我解决这个问题?努力寻找简明的指南。

最佳答案

要手动关闭弹出框,您需要保留对它的引用。一个好地方是在显示弹出框的 View Controller 中。

要让内容 View Controller 中的按钮告诉原始 View Controller (显示弹出框)关闭弹出框,两种可能的方式是委托(delegate)+协议(protocol)或 NSNotificationCenter。

下面的示例代码使用 NSNotificationCenter .

在呈现弹出框(“PresenterViewController”)的 View Controller 的 .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];
}

在内容 View Controller 中,实现按钮点击方法如下:
- (IBAction)dismissButtonTapped
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"contentFooViewControllerDone" object:self];
}

关于cocoa-touch - 如何设置 Popover View 以正确关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4937270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com