Make UITabBar wait for user input before switching tab

。_饼干妹妹 提交于 2019-12-11 05:45:48

问题


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.

I currently have this code:

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/52278057/make-uitabbar-wait-for-user-input-before-switching-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!