![](/img/trans.png)
[英]How to get notified when a presented view controller is dismissed with a gesture?
[英]Get notified of dismissed PopupDialog in view controller
我有一個項目,使用https://github.com/Orderella/PopupDialog的彈出對話框非常有效。
創建一個對話框,如下所示:
let ratingVC = PopupViewController(nibName: "PopupViewController", bundle: nil)
ratingVC.apiKey = self.apiKey
ratingVC.accountNumberString = accountNumberString
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)
ratingVC.presentedPopup = popup
self.present(popup, animated: true, completion: nil)
這允許自定義視圖控制器在彈出窗口中工作。 在PopupViewController
,可以使用self.dismiss(animated: true)
來self.dismiss(animated: true)
這很好,但是我不確定啟動視圖控制器(運行self.present的位置)將如何通知PopupDialog已被關閉。
我努力了
override func dismiss(animated flag: Bool, completion: (() -> Void)?)
{
super.dismiss(animated: flag, completion:completion)
}
在啟動視圖控制器中,但這不會被調用。
您可以創建一個PopupViewControllerDelegate
就像這個SO答案中描述的那樣,類似這樣。
protocol PopupViewControllerDelegate:class {
func viewControllerDidDismiss(_ sender: PopupViewController)
}
class PopupViewController: UIViewController {
...
weak var delegate: PopupViewControllerDelegate?
...
}
並在關閉ViewController時調用它。
然后在啟動的視圖控制器中實現PopupViewControllerDelegate
協議,並在創建PopupViewController時對其進行設置:
let ratingVC = PopupViewController(nibName: "PopupViewController", bundle: nil)
ratingVC.delegate = self
...
根據您使用的PopupDialog
的文檔,彈出按鈕具有完成塊,您可以在其中捕獲對話框是否要關閉。
或現在最簡單的方法是在PopupDialog
實例化中添加完成塊。
像這樣:
從:
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)
至:
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true) {
print("PopupDialog has been dismissed! ✅")
}
讓我知道這是否有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.