在切换选项卡之前让 UITabBar 等待用户输入
Make UITabBar wait for user input before switching tab
我有一个有 5 个标签的 UITabBar
。选择中间选项卡后,我希望弹出 UIAlertController
操作 Sheet,等待用户操作,然后在用户从 Sheet 中选择后加载新视图,因为我需要数据从 Sheet 正确加载视图。
我目前有这个代码:
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 中选择任何内容之前加载新视图外,这在所有方面都有效。是否可以让 UITabBar
等待操作,还是我需要换一种方式?
逻辑是始终在委托中 return 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 变量。
我有一个有 5 个标签的 UITabBar
。选择中间选项卡后,我希望弹出 UIAlertController
操作 Sheet,等待用户操作,然后在用户从 Sheet 中选择后加载新视图,因为我需要数据从 Sheet 正确加载视图。
我目前有这个代码:
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 中选择任何内容之前加载新视图外,这在所有方面都有效。是否可以让 UITabBar
等待操作,还是我需要换一种方式?
逻辑是始终在委托中 return 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 变量。