gpt4 book ai didi

ios - 在切换选项卡之前让 UITabBar 等待用户输入

转载 作者:行者123 更新时间:2023-11-28 05:52:53 25 4
gpt4 key购买 nike

我有一个带有 5 个选项卡的 UITabBar。选择中间选项卡时,我希望弹出一个 UIAlertController 操作表,等待用户操作,然后在用户从工作表中选择后加载新 View ,因为我需要加载工作表中的数据 View 正确。

我目前有这段代码:

extension CustomTabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let index = viewControllers?.index(of: viewController) else {
return false
}
if index == 2 {
var choice = CreateChoice()

let alert = UIAlertController(title: "Select Creation Type", message: "Please select the desired creation type", preferredStyle: .actionSheet)

let action1 = UIAlertAction(title: "Quick Create", style: .default) { (action:UIAlertAction) in
choice.choice = "quick"
}

let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
choice.choice = "cancel"
}

let action3 = UIAlertAction(title: "Full Create", style: .default) { (action:UIAlertAction) in
choice.choice = "full"
}

alert.addAction(action1)
alert.addAction(action2)
alert.addAction(action3)

present(alert, animated: true, completion: nil)
print(choice.choice)
if choice.choice == "cancel" {
return false
}
return true
}
return true
}
}

除了在用户从 Action Sheet 中选择任何内容之前加载新 View 外,这在所有方面都有效。是否可以让 UITabBar 等待操作,还是我需要换一种方式?

最佳答案

逻辑是始终在委托(delegate)中返回 false以编程方式更改为所需的索引,以防用户在警报中点击所需的操作。

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let index = viewControllers?.index(of: viewController) else {
return false
}
if index == 2 {

let alert = UIAlertController(title: "Select Creation Type", message: "Please select the desired creation type", preferredStyle: .actionSheet)

let action1 = UIAlertAction(title: "Quick Create", style: .default) { (action:UIAlertAction) in
tabBarController.selectedIndex = 2
}

let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
// Do nothing
}

let action3 = UIAlertAction(title: "Full Create", style: .default) { (action:UIAlertAction) in
tabBarController.selectedIndex = 2
}

alert.addAction(action1)
alert.addAction(action2)
alert.addAction(action3)

present(alert, animated: true, completion: nil)
return false
}
return true
}

顺便说一句,如果可行,您可以避免使用 choice 变量。

关于ios - 在切换选项卡之前让 UITabBar 等待用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278057/

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