Swift应该使用alertController选择ViewController

时间:2015-12-19 19:03:29

标签: swift uitabbarcontroller uialertcontroller

我有一个看似正常的用例:在选择某个标签页(' logout'在我的情况下)时,我想在转换前显示要求确认的提醒。这似乎应该在shouldSelectViewController函数中处理。但是,alertController是异步的,我强制在它完成之前返回一个布尔值。 shouldSelectViewController在完成之前等待异步任务似乎不太可能。在切换视图之前,我有更好的地方触发确认提醒吗?

这是我的代码:

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    if (viewController is Trove.HomeViewController) {
        var shouldSelect = false
        let alertController = UIAlertController(title: "Logout", message: "Are you sure you want to log out?", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "Logout", style: .Default) { (action) in
            shouldSelect = true
        }
        alertController.addAction(okAction)
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in }
        alertController.addAction(cancelAction)
        tabBarController.presentViewController(alertController, animated: true) { return shouldSelect}
    }
    return true
}

非常感谢任何帮助!谢谢!

1 个答案:

答案 0 :(得分:2)

您可以尝试设置selecedIndex的{​​{1}}属性。

您的新UITabBarController

if statement