首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何弹出视图控制器并显示UIAlertController

如何弹出视图控制器并显示UIAlertController
EN

Stack Overflow用户
提问于 2018-07-27 03:28:17
回答 1查看 700关注 0票数 1

假设ViewController2中有一个错误,我想返回到上一个视图控制器ViewController1,然后显示一个警告。现在如果我把这个放到ViewController2中

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

}

将其用作Alert类

    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)
        }

    }

}

它将抛出一个错误

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-27 03:39:42

问题是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)
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51546237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档