我想提醒用戶保存當前視圖控制器的變化如何在離開TabBarController中的當前ViewController之前顯示警報?
比方說,我有這樣的事情:
這裏,裏面TabBarController和導航控制器裏面我有一個「收藏夾」選項卡。我想要顯示警報,如果用戶切換到「聯繫人」
問題是警報顯示在目標ViewController(聯繫人)上方,因此它對用戶來說看起來很奇怪。
測試的解決方案:
第一,我試圖用
override func viewWillDisappear(animated: Bool) {
self.leavingAlert()
}
//inside FavoritesViewController
接下來,我想:
class FavoritesViewController: UIViewController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
}
func leavingAlert(){
let alert = UIAlertController(title: "Alert", message: "You forgot to do something here", preferredStyle: UIAlertControllerStyle.Alert)
let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(alertAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
self.leavingAlert()
}
}
效果相同
然後,我試圖去夠事件通過TabBarViewController:
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if let navigationController = selectedViewController as? UINavigationController {
if let activeController = navigationController.visibleViewController as? FavoritesViewController {
activeController.leavingAlert()
}
}
}
}
還有一次 - 同樣的效果。
請注意,我不打算中斷此UITabBarController塞格。這個想法只是問「保存還是不保存?」,如果「保存」,然後做一些東西,並繼續切換標籤,如果「不保存」 - 切換標籤立即。
謝謝你的幫助。如果在Obj-C中有一個解決方案,請也回答,我會試着去理解這個想法。
從ViewWillDisappear方法調用警報。 – iMuzahid
是的,你可以在我的問題中看到這個嘗試)) –