【问题标题】:Unable to dismiss UIViewController when it is presenting UIAlertController呈现 UIAlertController 时无法关闭 UIViewController
【发布时间】:2020-10-01 09:50:52
【问题描述】:

我正在尝试关闭当前正在呈现UIAlertControllerUIViewController,如下所示,

class SampleViewController: UIViewController {

    private var alertController: UIAlertController?

    override func viewDidLoad() {
        super.viewDidLoad()
        presentAlert()
    }

    fucn presentAlert() {
        let alertController = UIAlertController(title: "alert", message: nil, preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "Dismiss", style: .destructive, handler: {
            dismiss(animated: true)
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        present(alertController, animated: true, completion: {
            self.alertController = alertController
        })
    }

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {

        // alertController?.dismiss(animated: true)
        /*This dismisses SampleViewController when user taps Cancel
         but I want user to stay on this screen when they tap Cancel*/

        super.dismiss(animated: flag, completion: completion)
    }
}

class currentViewController: UIViewController {
    private let sampleViewController = SampleViewController()

    func presentSampleViewController() {
        present(sampleViewController, animated: true)
    }

    func dismissSampleViewController() {
        sampleViewController.dismiss(animated: true)
    }
}

但只有警报被解除,而不是整个SampleViewController 我也无法找到是否从CurrentViewControllerUIAlertController 操作调用override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)

我想同时关闭alertSampleViewController 并将用户带回CurrentViewController。任何帮助将不胜感激,在此先感谢。

【问题讨论】:

    标签: ios swift xcode uiviewcontroller


    【解决方案1】:

    dismissSampleViewController() 中调用dismiss(animated: flag, completion: completion) 而不是sampleViewController.dismiss(animated: true)

    通过关闭CurrentViewController,您将关闭它的所有子视图控制器

    【讨论】:

    • dismissSampleViewController 中调用dismiss(animated: flag, completion: completion) 而不是sampleViewController.dismiss(animated: true)
    • 请注意我已经提到我想同时关闭alertSampleViewController 并将用户带回CurrentViewController
    • 正确,我认为我提到的应该有效?通过关闭CurrentViewController,您将关闭它的所有子视图控制器。
    • 是的,这行得通,感谢您的澄清。请更新您的答案,以便我接受。
    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多