[英]Storyboard popover dismissed, delegate methods not called
我有一个视图控制器,使用故事板segue在弹出框中显示。
在呈现视图控制器中,我有以下代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let svc = segue.destinationViewController as? SettingsViewController {
svc.popoverPresentationController?.delegate = self
}
}
然而,事实证明,所呈现的视图控制器,即使它显示为酥料饼,具有modalPresentationStyle
的'.Modal
,并因此具有nil
popoverPresentationController
。 奇怪的!
所以,我更新了代码如下:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let svc = segue.destinationViewController as? SettingsViewController {
svc.modalPresentationStyle = .Popover
svc.popoverPresentationController?.delegate = self
}
}
svc.popoverPresentationController
委托现在设置为OK,但是如果用户在外面点击弹出窗口,则UIPopoverPresentationControllerDelegate
委托方法都没有被调用(例如, popoverPresentationControllerShouldDismissPopover
被调用。我错过了什么?
在这种情况下无需授权。 如果presentingViewController
(无论vc
包含popover)只是覆盖:
斯威夫特4
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
print("Dismiss: \(String(describing: self.presentedViewController))")
super.dismiss(animated: flag, completion: completion)
}
斯威夫特3
override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
// Before calling super get a handle on which controller is being dismissed
print("Dismiss: \(self.presentedViewController)")
super.dismissViewControllerAnimated(flag, completion: completion)
}
无论如何解雇,您都会收到通知。 您也不需要在prepareForSegue:
设置任何其他变量/设置prepareForSegue:
至少要处理此交互)。
进入相同的问题,阅读完文档后,我意识到你需要打电话:
[self presentViewController:myPopoverViewController animated: YES completion: nil];
为了调用委托方法。
完整代码段如下所示,并在我的 - (void)prepareForSegue:sender方法中运行:
// Present the view controller using the popover style.
myPopoverViewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:myPopoverViewController animated: YES completion: nil];
// Get the popover presentation controller and configure it.
UIPopoverPresentationController *presentationController =
[myPopoverViewController popoverPresentationController];
presentationController.permittedArrowDirections =
UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = myView;
presentationController.sourceRect = sourceRect;
https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.