如何在swift中添加从我的appdelegate类调用的弹出窗口?

use*_*930 3 cocoa-touch uialertview ios swift uialertcontroller

我正在为我的ios应用程序开发一个谷歌登录教程,当我们无法登录用户到我的应用程序时,有一部分.

到目前为止,appDelegate.swift中的代码部分如下所示:

guard error == nil && data != nil else {
     // check for fundamental networking error
     print("error=\(error)")

     //lets put popup here that says cant connect to server

     GIDSignIn.sharedInstance().signOut()
     return
}
Run Code Online (Sandbox Code Playgroud)

现在而不是打印错误我想放置警告弹出窗口.我试着在那里写:

  let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
  alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
  self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

但接着说我接到xcode错误self.presentViewControllervalue of type AppDelegate has no member presentViewController.

在这种情况下如何显示警告弹出窗口?

小智 6

尝试使用此行: -

self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

在这里你需要的是一个viewController对象来呈现AlertController,希望这会帮助你:)


Imt*_*tee 6

斯威夫特 3

self.window?.rootViewController?.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)