如何从UIViewController的扩展中为两个不同的控制器返回两个不同的表示控制器?

时间:2016-04-15 06:58:25

标签: ios swift

这是我的扩展程序

extension UIViewController: UIViewControllerTransitioningDelegate {

    func presentAssignBookToClassesViewController(controller: BWAssignBookToClassesViewController) {

        controller.modalPresentationStyle = .Custom
        controller.transitioningDelegate = self
        controller.preferredContentSize = CGSizeMake(500, 575)

        presentViewController(controller, animated: true, completion: nil)
    }

    func presentSettingsStoryboard() {

        if let settingsController = UIStoryboard(name: "TeacherSettingsStoryboard", bundle: nil).instantiateInitialViewController() {

            settingsController.modalPresentationStyle = .Custom
            settingsController.transitioningDelegate = self
            settingsController.preferredContentSize = CGSizeMake(500, 575)

            presentViewController(settingsController, animated: true, completion: nil)
        }
    }

    //MARK: - UIViewControllerTransitioningDelegate

    public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {

        return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
}

presentationControllerForPresentedViewController:内,我需要返回BWOverlayPresentationControllerBWSettingsPresentationController,具体取决于调用的方法。怎么做到这一点?

3 个答案:

答案 0 :(得分:1)

public func presentationControllerForPresentedViewController(presented: UIViewController?, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {

    // You can create some property in presented/presenting viewController.
    // and check here to return specific viewContoller.

    if (presented.(somePropertyInViewController)) {
        return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    else {
        return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
}

答案 1 :(得分:1)

我建议您可以使用两个viewController对象创建一个BaseViewContoller,例如:BWOverlayPresentationController,BWSettingsPresentationController,并根据条件返回特定的视图控制器。

(?<=#)\d+

答案 2 :(得分:1)

您可以通过restorationIdentifier区分它们(您只需使用故事板设置):

//MARK: - UIViewControllerTransitioningDelegate

public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {

    if presented.restorationIdentifier == BWSettingsRestorationIdentifier {
        return BWSettingsPresentationController(presentedViewController: presented, presentingViewController: presenting)
    } else {
        return BWOverlayPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
}