2017-07-26 54 views
0

這是我如何執行過渡:如何從提供的控制器中訪問我的自定義UIPresentationController?

extension UIViewController: UIViewControllerTransitioningDelegate { 

    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { 
     return OverlayPresentationController(presentedViewController: presented, presenting: presenting) 
    } 

    func presentOverlayController(_ controller: UIViewController) { 

     controller.modalPresentationStyle = .custom 
     controller.transitioningDelegate = self 

     present(controller, animated: true) 
    } 
} 

然後我提出控制器(AlertVC)中在某些時候我需要訪問它的顯示控制器:

print(presentationController as? OverlayPresentationController) //nil 
print(presentationController) //is ok, UIPresentationController 

爲什麼?

出席情況:

let controller = AlertVC.instantiate() 
controller.update() 
presentOverlayController(controller) 

class AlertVC: UIViewController { 

    class func instantiate() -> AlertVC { 
     return UIStoryboard(name: "Alert", bundle: Bundle(for: LoginVC.classForCoder())).instantiateInitialViewController() as! AlertVC 
    } 

    func update() { 
     _ = view 

     print(presentationController as? OverlayPresentationController) //nil 
    } 
} 
+0

UIPresentationController有一個名爲containerView的屬性。它擁有演示文稿的視圖層次結構並呈現控制器 –

+0

聽起來很有希望...可以通過這種方式訪問​​我的演示文稿控制器嗎? –

+0

對不起,我不知道,我已經通過互聯網閱讀,所以我與你分享,你可以嘗試如果作品,那麼你的很多時間將被保存。祝你好運 –

回答

0

你得到所呈現當前視圖控制器的視圖控制器通過調用presentingViewController

// The view controller that presented this view controller (or its farthest ancestor.) 
self.presentingViewController 

如果您呈現視圖控制器是一個導航控制器,它返回一個UINavigationController。你可以像這樣獲得你需要的視圖控制器:

let presentingNVC = self.presentingViewController as? UINavigationViewController 
let neededVC = presentingNVC.viewControllers.last as? NeededViewController 
+0

@BartłomiejSemańczyk你在哪裏嘗試調用'presentsViewController'?如果您嘗試在'viewDidLoad:'中調用它,則可能該值尚未設置。 –

+0

@BartłomiejSemańczyk,你確定你在使用'present(controller,animated:true)'呈現的視圖控制器中調用它? –

+0

@BartłomiejSemańczyk我認爲你在'presentationController(forPresented:present:source:)'return'語句之上缺少'presentationController.delegate = self'' –

相關問題