【问题标题】:UIAlertController panel disappears before user can respondUIAlertController 面板在用户响应之前消失
【发布时间】:2017-01-13 22:44:43
【问题描述】:

我正在使用警报控制器在 catch 块中显示错误。但是,在它自行消失之前,用户几乎看不到它。我究竟做错了什么?这是我的代码。

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController is CancelInspectionViewController {
        persistentContainer.viewContext.rollback()
        self.dismiss(animated: true, completion: nil)
        return false
    } else if viewController is SubmitInspectionViewController {
        do {
            try persistentContainer.viewContext.save()
            self.dismiss(animated: true, completion: nil)
        } catch {
            _alertController = UIAlertController(title: "Error Saving", message: error.localizedDescription, preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            _alertController.addAction(defaultAction)
            present(_alertController, animated:true, completion: {
                self.dismiss(animated: true, completion: nil)
            })
        }

【问题讨论】:

    标签: ios swift controller alert uialertcontroller


    【解决方案1】:

    您的问题是 self.dismiss(animated: true, completion: nil) 在错误的地方被调用。您一提交 _alertController 就会调用它。一旦显示警报控制器,您实际上不需要调用它。 UIAlertAction 处理解除它。

    【讨论】:

    • 我不是在关闭警报面板,而是在关闭标签栏控制器。但是,我完全删除了完成块,警报面板仍然消失了。
    • 如果您将此代码移至 tabBarController:didSelectViewController: 会怎样。我只是想知道警报控制器是否被解除,因为您返回 true(在显示代码之后),这实际上会导致 SubmitInspectionViewController 被显示。
    • 我实际上返回的是假的。这就是我想要的,因为有一个错误。 submitInspectionViewController 永远不应该显示,它只是在那里,所以这段代码会运行,然后我关闭标签栏控制器。我曾经在 didSelect 中有这段代码,但它在那里也不起作用。
    【解决方案2】:

    我想通了!我误解了警报控制器的工作原理。我认为它会阻塞线程,直到用户响应;它不是。所以这个函数后面的代码会关闭警报面板。

    工作代码是在显示警报后返回 false。

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController is CancelInspectionViewController {
            persistentContainer.viewContext.rollback()
            self.dismiss(animated: true, completion: nil)
            return false
        } else if viewController is SubmitInspectionViewController {
            do {
                try persistentContainer.viewContext.save()
                self.dismiss(animated: true, completion: nil)
            } catch {
                let alertController = UIAlertController(title: "Error Saving", message: error.localizedDescription, preferredStyle: .alert)
                let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                alertController.addAction(defaultAction)
                present(alertController, animated:true, completion: nil)
                return false
            }
    

    【讨论】:

      【解决方案3】:

      删除 self.dismiss(animated: true, completion: nil)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多