【发布时间】:2016-07-18 08:45:15
【问题描述】:
我试图提醒用户保存当前视图控制器上的更改
比方说,我有这样的东西:
在这里,在 TabBarController 和 Navigation Controller 中,我有一个“收藏夹”选项卡。如果用户切换到“联系人”,我想显示警报
问题是警报显示在目标 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 Segue。这个想法只是问“保存还是不保存?”,如果“保存”,那么做一些事情并继续切换标签,如果“不保存” - 立即切换标签。
感谢您的帮助。如果 Obj-C 有解决方案,也请回答,我会尽力抓住这个想法。
【问题讨论】:
-
从 ViewWillDisappear 方法调用警报。
-
是的,你可以在我的问题中看到这个尝试))
-
我是这样做的show decision here
标签: ios objective-c swift uitabbarcontroller