【问题标题】:Present alert after dismissing View Controller关闭视图控制器后显示警报
【发布时间】:2020-10-02 07:15:13
【问题描述】:

我使用的是最新的XcodeSwift 版本。

我正在展示一个特定的View Controller,如下所示:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let contactViewController = storyboard.instantiateViewController(identifier: "contactViewController")
show(contactViewController, sender: self)

我将像这样解雇这个View Controller

self.presentingViewController?.dismiss(animated: true, completion: nil)

我想在关闭View Controller 后立即显示UIAlertController

这个:

self.presentingViewController?.dismiss(animated: true, completion: nil)

let alertMessage = UIAlertController(title: "Your message was sent", message: "", preferredStyle: .alert)
let alertButton = UIAlertAction(title: "Okay", style: UIAlertAction.Style.default)
alertMessage.addAction(alertButton)
self.present(alertMessage, animated: true, completion: nil)

...当然不起作用,因为我不能在已解散的View Controller 上显示UIAlertController

View Controller 被关闭后,呈现此UIAlertController 的最佳方式是什么?

【问题讨论】:

    标签: ios swift xcode uiviewcontroller uialertcontroller


    【解决方案1】:

    您可以通过像这样获取顶级控制器在完成处理程序中完成此操作

    self.presentingViewController?.dismiss(animated: true, completion: {
                let alertMessage = UIAlertController(title: "Your message was sent", message: "", preferredStyle: .alert)
                   let alertButton = UIAlertAction(title: "Okay", style: UIAlertAction.Style.default)
                   alertMessage.addAction(alertButton)
                UIApplication.getTopMostViewController()?.present(alertMessage, animated: true, completion: nil)
            })
    

    使用这个扩展

    extension UIApplication {
    
        class func getTopMostViewController() -> UIViewController? {
            let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
            if var topController = keyWindow?.rootViewController {
                while let presentedViewController = topController.presentedViewController {
                    topController = presentedViewController
                }
                return topController
            } else {
                return nil
            }
        }
    }
    

    【讨论】:

    • 第一次看到while let,这真的是我的书。当let presentedViewController 为 nil 时,这个 while 循环是否会终止(又名。topController 没有 presentViewController)。
    【解决方案2】:

    使用 Jawad Ali 的扩展,我们可以锚定当前呈现的 ViewController。

    如果您想稍后解除该警报,您可以在另一个完成处理程序中执行此操作,如下面的代码所示。就我而言,我将一首歌曲保存到一个播放列表并关闭此播放列表并显示一个短时间警报,让用户知道保存可以。

    DispatchQueue.main.async {
        self?.removeSpinner()
        self?.dismiss(animated: true, completion: {
            let alert = UIAlertController(title: "Save to playlist", message: nil, preferredStyle: .alert)
            UIApplication.getTopMostViewController()?.present(alert, animated: true, completion: {
                Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { _ in
                    alert.dismiss(animated: true)
                }
            })
        })
    }               
    

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多