视图控制器在被另一个视图控制器呈现后崩溃



由于某种原因,viewDidLoad()崩溃

fatal error: unexpectedly found nil while unwrapping an Optional value

在从另一个视图控制器呈现 UI 后设置 UI 时,在我的应用程序中authViewController

//authViewController
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
authState.addTarget(self, action: #selector(authStateChanged), for: .valueChanged)//<-- The place where it crashes
emailInput.addTarget(self, action: #selector(emailPasswordTextChanged), for: .editingChanged)
passwordInput.addTarget(self, action: #selector(emailPasswordTextChanged), for: .editingChanged)
SignIn.addTarget(self, action: #selector(signInPressed), for: .touchUpInside)
//Set actions
infoView.layer.cornerRadius = 5
SignIn.layer.cornerRadius = 5
//UI setup
}

另一个视图控制器:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
FIRAuth.auth()?.addStateDidChangeListener({ (auth, user) in
if user != nil {
}
else {
print("pop")
DispatchQueue.main.async {
self.present(authViewController(), animated: true, completion: nil)
}
}
})
}

如果你知道,谁能告诉我发生了什么以及为什么会发生?

而不是像这样呈现ViewController

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

实例化后尝试演示ViewController

let storyBoard = UIStoryboard(name: storyboardname, bundle: nil)
let authVC = storyBoard.instantiateViewController(withIdentifier: viewController Identifier) as? authViewController
self.present(authVC, animated: true, completion: nil)

我通过以下方式解决了它:

let authVC = self.storyboard?.instantiateViewController(withIdentifier: "authVC")
self.present(authVC!, animated: true, completion: nil)

最新更新