Make UITabBar wait for user input before switching

2019-08-07 22:02发布

问题:

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.