有什么方法可以从 swift 中的 AppDelegate 调用 gui 元素吗?

Is there any way of calling a gui element from AppDelegate in swift?

我正在我的移动 ios swift 应用程序中实施 google 登录功能,所有操作都由 AppDelegate.swift 处理。我想在登录过程中显示加载指示器,所以在我的 UIViewController 中,我将一个非常简单的代码附加到 google 登录按钮:

class ViewController: UIViewController, GIDSignInUIDelegate {

@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
@IBAction func googleSignInButtonAction(sender: AnyObject) {

    loadingIndicator.startAnimating()
}

override func viewDidLoad() {
    super.viewDidLoad()
    loadingIndicator.hidesWhenStopped = true
}

现在,当用户点击登录按钮时,它会自动显示旋转指示器并打开 Safari 视图以请求登录凭据。当用户输入它们时,他将再次被重定向到 UIViewController(现在旋转指示器打开 - 这很好)并且 AppDelegate 中的整个过程发生:

 func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
    withError error: NSError!) {
        if (error != nil) {
            print("Looks like we have a sign in error: \(error)")
        }else {
            //here I'm sending the google token to my backend server and in case of
            //success I'm redirecting user to the protected panel - 
            //main window of my app:

              let sb = UIStoryboard(name: "Main", bundle: nil)
              if let tabBarVC = sb.instantiateViewControllerWithIdentifier("TabController") as? TabController {
              self.window!.rootViewController = tabBarVC

          }

上面的代码发生在 AppDelegate,所以我的问题是 - 如何从那个地方参考 ViewController 中的加载指示器并在成功时停止旋转?

Per Google's docsGIDSignIn 单例有两个不同的委托属性。

Google 建议您应用的 AppDelegate 实施 GIDSignInDelegate 协议并使 AppDelegate 成为 delegate.

但是你忽略了还有一个GIDSignInUIDelegate。您适当的视图控制器应该实现 this protocol,并且很可能在他们的 viewWillAppear 方法上,将自己指定为 GIDSignIn 单例的 uiDelegate.