【问题标题】:How to pop view controller and then show UIAlertController如何弹出视图控制器然后显示 UIAlertController
【发布时间】:2018-07-26 19:28:16
【问题描述】:

假设 ViewController2 有错误,我想回到 ViewController1,即之前的视图控制器,然后显示警报。现在如果我把它放在 ViewController2 中

    @IBAction func testing(_ sender: Any) {
    navigationController?.popViewController(animated: true)
    Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: LandingViewController() as UIViewController)

}

将其用作警报类

    public class Alert {
    class func alert(userTitle: String?, userMessage: String, userOptions: String, in vc: UIViewController) {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: userTitle, message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: userOptions, style: UIAlertActionStyle.default, handler: nil))
            vc.present(alert, animated: true, completion: nil)
        }

    }

}

会报错

那么有没有办法做到这一点?

【问题讨论】:

  • 抛出什么错误?在代码的第 3 行中,您创建了一个视图控制器的新实例,而不是查找现有的实例。

标签: swift swift4 uialertcontroller


【解决方案1】:

问题是LandingViewController() as UIViewController没有在UiWindow中渲染

试试这个

guard let nav = navigationController, let top = nav.topViewController else {
    return
}

nav.popViewController(animated: true)
Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: top)

【讨论】:

  • 哦,哇,你不知道我找了多久。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2021-06-24
  • 1970-01-01
相关资源
最近更新 更多