在切换选项卡之前让 UITabBar 等待用户输入 [英] Make UITabBar wait for user input before switching tab

查看:19
本文介绍了在切换选项卡之前让 UITabBar 等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITabBar with 5 tabs. When the middle tab is selected I want a UIAlertController Action Sheet to pop up, await user action and then load the new view once the user has selected from the Sheet since I need the data from the Sheet to load the view correctly.

我目前有这个代码:

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
    }
} 

除了在用户从操作表中选择任何内容之前加载新视图之外,这适用于所有方式.是否可以让 UITabBar 等待操作,或者我是否需要以另一种方式进行处理?

This works in every way except the new view loads before the user selects anything from the Action Sheet. Is it possible to make the UITabBar wait for the action or do I need to go about this another way?

推荐答案

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

The logic is to always return false in the delegate and programatically change to the required index in case the user taps the required action in the alert.

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
}

顺便说一句,如果可行,您可以避免选择变量.

Btw you can avoid the choice variable if this works.

这篇关于在切换选项卡之前让 UITabBar 等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆