Заставьте UITabBar ждать ввода пользователя перед переключением вкладки - PullRequest
0 голосов
/ 11 сентября 2018

У меня есть UITabBar с 5 вкладками. Когда выбрана средняя вкладка, я хочу, чтобы всплыл лист действий UIAlertController, дождался действий пользователя и затем загрузил новый вид, как только пользователь выбрал лист, так как мне нужны данные с листа для корректной загрузки вида.

У меня сейчас есть этот код:

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 ждать действия или мне нужно по-другому это сделать?

1 Ответ

0 голосов
/ 11 сентября 2018

Логика состоит в том, чтобы всегда возвращать 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
}

Кстати, вы можете избежать выбора переменной, если это работает.

...