PopoverPresentationController没有调用委托方法

时间:2016-05-02 13:34:47

标签: ios swift uipopovercontroller

我有一个类在一个空的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时,委托方法popoverPresentationControllerDidDismissPopover永远不会被解雇。

我已将loginScreenPopover的委托设置为self,并让Popovers类继承自UIPopoverPresentationControllerDelegate,但似乎都没有工作。

如果与问题相关,则在iPhone上。

1 个答案:

答案 0 :(得分:0)

我找到了答案。在LogInScreen的ViewDidLoad()中,我将委托设置为:

self.popoverPresentationController.delegate = getPopovers()

getPopovers()函数是一个函数,用于检索代码中其他位置创建的Popover对象。现在在我的Popovers类中,我可以符合UIPopoverPresentationControllerDelegate并使用这些方法。