gpt4 book ai didi

Swift shouldSelectViewController 使用 alertController

转载 作者:可可西里 更新时间:2023-11-01 01:37:44 26 4
gpt4 key购买 nike

我有一个看似正常的用例:在选择某个选项卡(在我的例子中为“注销”)时,我想显示一个警告,要求在转换之前进行确认。这似乎应该在 shouldSelectViewController 函数中处理。但是,alertController 是异步的,我不得不在它完成之前返回一个 bool 值。 shouldSelectViewController 似乎不太可能在完成之前等待异步任务。有没有更好的地方让我在切换 View 之前触发我的确认警报?

这是我的代码:

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
}

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

最佳答案

您可以尝试设置 UITabBarControllerselecedIndex 属性。

您的新if 语句:

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
}

关于Swift shouldSelectViewController 使用 alertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com