故事板弹出窗口已关闭,未调用委托方法
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
}
}
然而,事实证明,呈现的视图控制器,即使它显示为弹出窗口,也具有 '.Modal
的 modalPresentationStyle
,因此 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
委托现在设置正常,但是如果用户在外部点击弹出窗口被关闭,UIPopoverPresentationControllerDelegate
委托方法的 none(例如 popoverPresentationControllerShouldDismissPopover
被调用了。我错过了什么?
这种情况下不需要委派。如果 presentingViewController
(无论 vc
包含弹出窗口)只是覆盖:
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:
中设置任何额外的 variables/settings(至少可以处理此交互)。
运行 遇到同样的问题,在阅读文档后,我意识到你需要调用:
[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
我有一个视图控制器,它使用故事板 segue 在弹出窗口中呈现。
在呈现视图控制器中,我有以下代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let svc = segue.destinationViewController as? SettingsViewController {
svc.popoverPresentationController?.delegate = self
}
}
然而,事实证明,呈现的视图控制器,即使它显示为弹出窗口,也具有 '.Modal
的 modalPresentationStyle
,因此 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
委托现在设置正常,但是如果用户在外部点击弹出窗口被关闭,UIPopoverPresentationControllerDelegate
委托方法的 none(例如 popoverPresentationControllerShouldDismissPopover
被调用了。我错过了什么?
这种情况下不需要委派。如果 presentingViewController
(无论 vc
包含弹出窗口)只是覆盖:
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:
中设置任何额外的 variables/settings(至少可以处理此交互)。
运行 遇到同样的问题,在阅读文档后,我意识到你需要调用:
[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