ios - 当我尝试登录或注册我的应用程序时,Swift 窗口层次结构错误?

标签 ios swift xcode window

我对窗口层次结构有疑问。当我尝试登录或注册并打开应用程序的主页时,我无法显示其他页面控制台错误:

Warning: Attempt to present on whose view is not in the window hierarchy!

但是当我使用同一用户重新打开我的应用程序时,一切正常。这是创建用户进程后的代码:

    func createUser(withEmail email: String, password: String, username: String) {

    Auth.auth().createUser(withEmail: email, password: password) { (result, error) in

        if let error = error {
            print("Failed to sign user up with error: ", error.localizedDescription)
            return
        }

        guard let uid = result?.user.uid else { return }

        let values = ["E-mail": email, "Kullanıcı Adı": username]

        Database.database().reference().child("users").child(uid).updateChildValues(values, withCompletionBlock: { (error, ref) in
            if let error = error {
                print("Failed to update database values with error: ", error.localizedDescription)
                return
            }

            let layout = UICollectionViewFlowLayout()
            let homeController = HomeController(collectionViewLayout: layout)
            let navController = UINavigationController(rootViewController: homeController)
            self.present(navController, animated: false, completion: nil)
        })

    }

}

这里是调用top函数的函数:

    @objc func handleSignUp() {
    guard let email = emailTextField.text else { return }
    guard let password = passwordTextField.text else { return }
    guard let username = usernameTextField.text else { return }

    createUser(withEmail: email, password: password, username: username)
}

最佳答案

也许你的 rootViewController 正在呈现另一个 ViewController。 您可以尝试:

if let presentedVC = self.presentedViewController {
    presentedVC.present(navController, animated: false, completion: nil)
} else {
    self.present(navController, animated: false, completion: nil)
}

关于ios - 当我尝试登录或注册我的应用程序时,Swift 窗口层次结构错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030082/

相关文章:

ios - 一小段代码 Swift 错误 : not convertible to PFUserResultBlock

ios - Jsonobject + 图像多部分 AFNetworking

ios - Braintree 嵌入式 UI (iOS)

objective-c - 如何禁用前向类编译器警告(未记录的类)

ios - 当 View 出现时以编程方式定义 UILabel 的大小

ios - 在应用程序启动时启动视频失败 iOS

ios - Swift 4.2 - 获取编译器错误以桥接 FacebookCore 中缺少的头文件。有任何想法吗?

ios - 应用程序图标未显示在 iPhone 上 - Xcode 9.1

ios - 如何计算和使用 "textFieldDidBeginEditing"中的键盘高度(快速),以便我可以精确地将 UITextfield 向上移动?

ios - 如何在 Swift 中实现 "share button"