【问题标题】:Alert not showing when presented to view controller that is having webview当呈现给具有 webview 的视图控制器时不显示警报
【发布时间】:2019-07-18 06:23:03
【问题描述】:

我正在尝试在我的视图控制器中显示警报框,该控制器正在使用 WKWebView 进行 web 视图。我有一个侧边菜单,我在另一个继承 UIView 的类中创建,并且侧边菜单有一个注销选项。所以我希望尽快当我点击注销选项时,侧边菜单应该从 superview 中删除并出现警报。

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

     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)

     }

一旦点击注销选项,就会出现警报。

【问题讨论】:

  • 如何在视图中显示 WKWebview?展示它还是推动它?
  • 我在视图控制器中显示 wkwebview,但我的功能在另一个继承 UIView 的类中,并希望在 UIView 中发生注销单击后将警报显示到 Viewcontroller

标签: swift wkwebview


【解决方案1】:

您的 VC(即 WebViewController)尚未呈现。

在当前 ViewController 上使用 self.present(...) 方法,例如:

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

    self.present(alert, animated: true, completion: nil)

     }  

【讨论】:

  • 我在另一个类中使用这个函数,这就是为什么我要创建那个类的对象然后呈现
【解决方案2】:

问题来了

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)

【讨论】:

  • 实现您的代码后出现此错误:无法将类型“WebViewController.Type”的值转换为预期的参数类型“WebViewController”
  • WebViewController 是 UIViewController 而 SideBar 是 UIVIEW
  • @SmartCoder 您只需将 UIViewController 对象传递给您需要显示警报的位置
  • 请帮忙举个例子
猜你喜欢
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 2020-02-13
  • 1970-01-01
  • 2018-06-01
相关资源
最近更新 更多