在下面所示的项目中,有一个InitialViewController有一个标记为“Show Popover”的按钮。当点击该按钮时,应用程序应该将第二个视图控制器(PopoverViewController)显示为弹出窗口。第二个视图控制器只有一个标签,上面写着“Popover!”.
swift - 可以将显示为弹出窗口的UIViewController用作其自己的popoverPresentationController委托(delegate)吗?-LMLPHP
如果InitialViewController负责实例化PopoverViewController,检索popoverPresentationController,然后将popoverPresentationController的delegate设置为自身(toInitialViewController),则此操作可以正常工作。结果如下:
swift - 可以将显示为弹出窗口的UIViewController用作其自己的popoverPresentationController委托(delegate)吗?-LMLPHP
但是,对于最大的可重用性,如果InitialViewController不需要知道呈现控制器是如何被委派的话,那就太好了。我认为PopoverViewController应该可以将自己设置为popopompressioncontroller的delegate。我已经在viewDidLoadviewWillAppear函数或PopoverViewController函数中尝试过。然而,这两种情况下的PopoverViewController都是模态的,如下所示:
swift - 可以将显示为弹出窗口的UIViewController用作其自己的popoverPresentationController委托(delegate)吗?-LMLPHP
所有代码都包含在InitialViewControllerPopoverViewController中。失败版本的InitialViewController中使用的代码如下所示:

import UIKit

// MARK: - UIViewController subclass

class InitialViewController: UIViewController {

    struct Lets {
        static let storyboardName = "Main"
        static let popoverStoryboardID = "Popover View Controller"
    }

    @IBAction func showPopoverButton(_ sender: UIButton) {

        // instantiate & present the popover view controller
        let storyboard = UIStoryboard(name: Lets.storyboardName,
                                      bundle: nil )
        let popoverViewController =
            storyboard.instantiateViewController(withIdentifier: Lets.popoverStoryboardID )
        popoverViewController.modalPresentationStyle = .popover
        guard let popoverPresenter = popoverViewController.popoverPresentationController
            else {
                fatalError( "could not retrieve a pointer to the 'popoverPresentationController' property of popoverViewController")
        }
        present(popoverViewController,
                animated: true,
                completion: nil )

        // Retrieve and configure UIPopoverPresentationController
        // after presentation (per
        // https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller)

        popoverPresenter.permittedArrowDirections = .any
        let button = sender
        popoverPresenter.sourceView = button
        popoverPresenter.sourceRect = button.bounds

    }
}

失败PopoverViewController中的代码如下所示:
import UIKit


// MARK: - main UIViewController subclass

class PopoverViewController: UIViewController {

    // MARK: API
    var factorForMarginsAroundButton: CGFloat = 1.2

    // MARK: outlets and actions
    @IBOutlet weak var popoverLabel: UILabel!

    // MARK: lifecycle

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear( animated )

        // set the preferred size for popover presentations
        let labelSize =
            popoverLabel.systemLayoutSizeFitting( UILayoutFittingCompressedSize )
        let labelWithMargins =
            CGSize(width: labelSize.width * factorForMarginsAroundButton,
                   height: labelSize.height * factorForMarginsAroundButton )
        preferredContentSize = labelWithMargins

        // set the delegate for the popoverPresentationController to self
        popoverPresentationController?.delegate = self

    }
}
// MARK: - UIPopoverPresentationControllerDelegate
//       (inherits from protocol UIAdaptivePresentationControllerDelegate)

extension PopoverViewController: UIPopoverPresentationControllerDelegate
{
    func adaptivePresentationStyle(for controller: UIPresentationController,
                                   traitCollection: UITraitCollection)
        -> UIModalPresentationStyle{
            return .none
    }
}

作为弹出窗口显示的视图控制器是否可以作为其自己的代理?
我使用的是Xcode8.0和Swift3.1,目标是iOS10.0

最佳答案

当然有可能。你在处理时间问题。您需要在viewWillAppear之前设置代理。不幸的是,没有方便的视图生命周期函数来插入分配,所以我改为这样做。
PopoverViewController类中,在重写的getter中分配委托。如果你愿意的话,你可以把作业设为有条件的。这将创建一个永久关系,因此其他代码永远不会通过分配委托来“重写”委托。

override var popoverPresentationController: UIPopoverPresentationController? {
    get {
        let ppc = super.popoverPresentationController
        ppc?.delegate = self
        return ppc
    }
}

关于swift - 可以将显示为弹出窗口的UIViewController用作其自己的popoverPresentationController委托(delegate)吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45469066/

10-12 14:39