我有一个类可以在空的 swift 文件中构造 UIPopoverPresentationController:
import Foundation
import UIKit
class Popovers : NSObject, UIPopoverPresentationControllerDelegate {
func presentLoginScreen() {
// Presenting login Popover
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginScreen = storyboard.instantiateViewControllerWithIdentifier("LogInScreen")
let window = UIApplication.sharedApplication().windows.last!
let height = window.screen.bounds.size.height
let width = window.screen.bounds.size.width
loginScreen.modalPresentationStyle = UIModalPresentationStyle.Popover
loginScreen.preferredContentSize = CGSizeMake(width * 0.85 , height * 0.35)
let loginScreenPopover = loginScreen.popoverPresentationController
loginScreenPopover?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
loginScreenPopover?.delegate = self
loginScreenPopover?.sourceView = window.rootViewController?.view
loginScreenPopover?.sourceRect = CGRectMake(CGRectGetMidX(window.screen.bounds), CGRectGetMidY(window.screen.bounds), 0, 0)
window.rootViewController?.presentViewController(loginScreen, animated: true, completion: nil)
let blur = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
let bounds = window.screen.bounds
blur.frame = bounds
blur.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
window.rootViewController!.view.addSubview(blur)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
// Remove blur layer
print("method called")
let window = UIApplication.sharedApplication().windows.last
window?.subviews.last?.removeFromSuperview()
}
}
但是每当我关闭 PopoverPresentationController 时,委托(delegate)方法 popoverPresentationControllerDidDismissPopover
就永远不会被触发。
我已将 loginScreenPopover 的委托(delegate)设置为 self,并让 Popovers 类继承自 UIPopoverPresentationControllerDelegate,但似乎都不起作用。
它是在 iPhone 上,如果这与问题相关的话。
最佳答案
我已经找到答案了。在 LogInScreen 的 ViewDidLoad() 中,我将委托(delegate)设置为:
self.popoverPresentationController.delegate = getPopovers()
其中 getPopovers()
函数是用于检索在代码中其他位置创建的 Popover 对象的函数。现在,在我的 Popovers 类中,我可以遵循 UIPopoverPresentationControllerDelegate 并使用这些方法。
关于ios - PopoverPresentationController 委托(delegate)方法未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983761/