gpt4 book ai didi

ios - 当尝试注销/放松回到主屏幕时,如果甚至没有 optional ,为什么会收到“找到零”错误?

转载 作者:行者123 更新时间:2023-11-29 05:08:35 24 4
gpt4 key购买 nike

错误所在行:

self.passwordField.delegate = self

按钮中的代码:

 @IBAction func unwindToRed(_ sender: Any) {

do {
try Auth.auth().signOut()
let ViewController1 = ViewController()
let ViewNavigationController = UINavigationController(rootViewController: ViewController1)
self.present(ViewNavigationController, animated: true, completion: nil)
} catch let err {
print(err)
}

}

这是相关的主页代码:

class ViewController: UIViewController, UITextFieldDelegate {



@IBOutlet weak var emailField: UITextField!

@IBOutlet weak var passwordField: UITextField!

var userUID: String!
var databaseRef: DatabaseReference!

override func viewDidLoad() {

super.viewDidLoad()

databaseRef = Database.database().reference()


self.passwordField.delegate = self
self.emailField.delegate = self

emailField.attributedPlaceholder = NSAttributedString(string: "Email",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])

passwordField.attributedPlaceholder = NSAttributedString(string: "Password",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])



}
override func viewDidAppear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: "uid") {
self.performSegue(withIdentifier: "tohome", sender: nil)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func signInPressed(_ sender: Any) {

Auth.auth().createUser(withEmail: (emailField.text ?? ""), password: (passwordField.text ?? "")) { (user, error) in
if let _eror = error {
//something bad happning
print(_eror.localizedDescription )
let alert = UIAlertController(title: "Error", message: "Invalid Entry or Duplicate.", preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)

}else{
//user registered successfully
print(user as Any)
if let userID = user?.uid {
KeychainWrapper.standard.set((userID), forKey: "uid")

let databaseRef = Database.database().reference()


databaseRef.child("people").child(userID).child("users").setValue(self.emailField.text!)

databaseRef.child("people").child(userID).child("postID").setValue(userID)

self.performSegue(withIdentifier: "tohome", sender: nil)
}

}
}
}

@IBAction func loginInPressed(_ sender: Any) {
Auth.auth().signIn(withEmail: (emailField.text ?? ""), password: (passwordField.text ?? "")) { (user, error) in
if let _eror = error {
//something bad happning
print(_eror.localizedDescription )
let alert = UIAlertController(title: "Error", message: "Incorrect Email or Password.", preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)

}else{
//user registered successfully
print(user as Any)
if let userID = user?.uid {
KeychainWrapper.standard.set((userID), forKey: "uid")
self.performSegue(withIdentifier: "tohome", sender: nil) }

}
}
}

最佳答案

问题隐藏在这一行:

@IBAction func unwindToRed(_ sender: Any) {
do {
try Auth.auth().signOut()
let ViewController1 = ViewController() // <-- This is the problem
let ViewNavigationController = UINavigationController(rootViewController: ViewController1)
self.present(ViewNavigationController, animated: true, completion: nil)
} catch let err {
print(err)
}

}

因为您使用 Storyboard来创建 View ,所以您应该从 Storyboard实例化您的 View Controller 。要正确执行此操作,请参阅 Creating View Controllers from Storyboard

如果您是 iOS 开发新手或者您不知道为什么需要这样做,请参阅此 post这将解释从 Storyboard实例化 View Controller 与创建新实例。

关于ios - 当尝试注销/放松回到主屏幕时,如果甚至没有 optional ,为什么会收到“找到零”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59913848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com