【问题标题】:How to display an Alert before leaving current ViewController in TabBarController?如何在 TabBarController 中离开当前 ViewController 之前显示警报?
【发布时间】: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


【解决方案1】:

您可以为UITabBarController创建delegate,并重载方法:

optional func tabBarController(_ tabBarController: UITabBarController,
    shouldSelectViewController viewController: UIViewController) -> Bool

如果用户尝试切换到 viewController(从 FavoritesViewController),您会从此方法返回 false 并显示警报。
在警报的回调之一中,您可以切换到目标programmatically

unowned(unsafe) var selectedViewController: UIViewController?

【讨论】:

    【解决方案2】:

    你应该继承你的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
        }
    }
    

    【讨论】:

      【解决方案3】:

      解决方案之一是用常规 IBAction 替换 segue。您的按钮联系人必须在显示警报的“touch up inside”事件上调用 IBAction,然后在警报完成处理程序中调用控制器的 performSegueWithIdentifier 方法。

      【讨论】:

        猜你喜欢
        • 2019-12-24
        • 2018-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多