我有一个使用故事板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
)都不会被调用。我是什么人丢失?
答案 0 :(得分:7)
在这种情况下无需代表团。如果presentingViewController
(任何vc
包含popover)只是覆盖:
Swift 4
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
print("Dismiss: \(String(describing: self.presentedViewController))")
super.dismiss(animated: flag, completion: completion)
}
Swift 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:
中设置任何其他变量/设置(至少可以处理此交互)。
答案 1 :(得分:0)
遇到同样的问题,在阅读完文档后,我意识到你需要打电话:
[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