[英]How to display an Alert before leaving current ViewController in TabBarController?
我想提醒用户将更改保存在当前View Controller上
比方说,我有这样的事情:
在TabBarController和导航控制器中,我有一个“收藏夹”选项卡。 如果用户切换到“联系人”,我想显示一个警报
问题是Alert显示在目标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中有解决方案,也请回答,我会尽力抓住这个想法。
您可以为UITabBarController和重载方法创建委托 :
optional func tabBarController(_ tabBarController: UITabBarController,
shouldSelectViewController viewController: UIViewController) -> Bool
如果用户尝试切换到viewController(从收藏夹视图控制器),则从此方法返回false并显示警报。
在警报的回调之一中,可以以编程方式切换到目标。
unowned(unsafe) var selectedViewController: UIViewController?
您应该将UITabBarController
子类化,并使其成为自己的委托。 这样的事情应该工作:
class TabBarController: UITabBarController {
var viewControllerToSelect: UIViewController?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
delegate = self
}
func showLeavingAlert() {
let leavingAlert = UIAlertController(title: "Warning", message: "Do you want to save before you leave?", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Yes", style: .Default) { (action) in
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
// switch viewcontroller after one second delay (faked save action)
self.performSwitch()
}
}
leavingAlert.addAction(saveAction)
let cancelAction = UIAlertAction(title: "No", style: .Cancel) { (action) in
// switch viewcontroller immediately
self.performSwitch()
}
leavingAlert.addAction(cancelAction)
presentViewController(leavingAlert, animated: true, completion: nil)
}
func performSwitch() {
if let viewControllerToSelect = viewControllerToSelect {
// switch viewcontroller immediately
selectedViewController = viewControllerToSelect
// reset reference
self.viewControllerToSelect = nil
}
}
}
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if let navigationController = selectedViewController as? UINavigationController, _ = navigationController.visibleViewController as? FavoritesViewController {
// save a reference to the viewcontroller the user wants to switch to
viewControllerToSelect = viewController
// present the alert
showLeavingAlert()
// return false so that the tabs do not get switched immediately
return false
}
return true
}
}
解决方案之一是用常规IBAction代替segue。 您的按钮联系人必须在显示警报的“内部触摸”事件上调用IBAction,然后在警报完成处理程序中调用控制器的performSegueWithIdentifier方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.