Swift shouldSelectViewController 使用 alertController
Swift shouldSelectViewController using alertController
我有一个看起来很正常的用例:在选择某个选项卡时('logout' 在我的例子中),我想在转换之前显示一个要求确认的警报。这似乎应该在 shouldSelectViewController
函数中处理。但是,alertController
是异步的,我不得不在它完成之前 return 一个布尔值。 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
}
非常感谢任何帮助!谢谢!
您可以尝试设置 selecedIndex
属性 的 UITabBarController
。
你的新 if statement
:
if (viewController is Trove.HomeViewController) {
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
tabBarController.selectedIndex = 0 //CHANGE ME
}
alertController.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in }
alertController.addAction(cancelAction)
tabBarController.presentViewController(alertController, animated: true)
return false
}
我有一个看起来很正常的用例:在选择某个选项卡时('logout' 在我的例子中),我想在转换之前显示一个要求确认的警报。这似乎应该在 shouldSelectViewController
函数中处理。但是,alertController
是异步的,我不得不在它完成之前 return 一个布尔值。 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
}
非常感谢任何帮助!谢谢!
您可以尝试设置 selecedIndex
属性 的 UITabBarController
。
你的新 if statement
:
if (viewController is Trove.HomeViewController) {
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
tabBarController.selectedIndex = 0 //CHANGE ME
}
alertController.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in }
alertController.addAction(cancelAction)
tabBarController.presentViewController(alertController, animated: true)
return false
}