首页 > 解决方案 > 当呈现给具有 webview 的视图控制器时不显示警报

问题描述

我试图在我的视图控制器中显示警报框,它有一个使用 WKWebView 的 web 视图。我有一个侧边菜单,我在另一个继承 UIView 的类中创建,并且该侧边菜单有一个注销选项。所以我想要它只要我点击在注销选项上,侧面菜单应从超级视图中删除,并发出警报。

我已经创建了sidemenu并在单击选项时删除了视图。现在我只想显示警报。但这没有发生。

     func logout(){
    let alert = UIAlertController(title: "", message: "Do you want to logout from the application", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
    let vc = WebViewController()
    vc.present(alert, animated: true, completion: nil)

     }

单击注销选项后,应立即出现警报。

标签: swiftwkwebview

解决方案


当你写的时候问题就在这里

let vc = WebViewController()

创建 WebViewController() 的新对象,以便警报出现在您创建的新对象中,而不是在前一个对象中创建或从其他视图调用函数 logout()

所以你需要传递 UIViewController 的对象,你需要在其中显示警报

 func logout(vc: UIViewController){
    let alert = UIAlertController(title: "", message: "Do you want to logout from the application", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
    vc.present(alert, animated: true, completion: nil)

     }

怎么打电话

logout(vc: self)

推荐阅读