使用-> present调用后,rootViewController无法加载(viewControllerToPresent:UIViewController,动画:true,完成:nil)

时间:2018-12-16 04:18:04

标签: ios swift uiviewcontroller rootviewcontroller

我的rootViewController工作正常并且加载正常。但是,当我从另一个viewController使用present(viewControllerToPresent: UIViewController, animated: true, completion: nil)调用它时,除了黑屏什么都没有。

我到处都是stackOverflow,但只找到了故事板用户的解决方案。我正在以编程方式进行

@objc func handleLogin() {
    print("LOGIN BUTTON TOUCHED")
    guard let email = emailTextField.text, let password = passwordTextField.text else {
        print("Form is not valid.")
        return
    }
    Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
        if error != nil {
            print(error!)
            return
        }
        let viewController = RootViewController()
   ***  self.present(viewController, animated: true, completion: nil)
        print("Logged in")            
    }
}

2 个答案:

答案 0 :(得分:1)

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

没什么问题

我认为您的RootViewController()已经提交。您看到的黑屏可能是没有任何数据的黑屏?我不确定,该类是否需要您的代码。

您可以考虑的另一种方法是像这样从当前rootViewController替换真实的uiWindow

@objc func handleLogin() {
  print("LOGIN BUTTON TOUCHED")
  guard let email = emailTextField.text, let password = passwordTextField.text else {
    print("Form is not valid.")
    return
  }

  Auth.auth().signIn(withEmail: email, password: password) { [weak self] (user, error) in
    guard let strongSelf = self else { return }
    guard error == nil else { return }
    guard let user = user else { return }

    UIApplication.shared.keyWindow?.rootViewController = RootViewController()
    print("Logged in")            
  }
}

答案 1 :(得分:0)

您已经创建了一个新实例(RootViewController),并且它必须是黑色的。.您未引用您的RootViewController