'Getting Fatal Error with UITextFieldDelegete
I have setup a UITextField in a ViewController and Xcode doesn't show me errors. But when I run my app and load the Screen the app crashes with the error message:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
This is my code:
import Foundation
import UIKit
class LoginScreenViewController: UIViewController, UITextFieldDelegate{
@IBOutlet weak var phoneAndEmailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
phoneAndEmailTextField.delegate = self
passwordTextField.delegate = self
}
@IBAction func phoneAndEmailTextFieldAction(_ sender: UITextField) {
phoneAndEmailTextField.endEditing(true)
print(phoneAndEmailTextField.text!)
}
@IBAction func passwordTextFieldAction(_ sender: UITextField) {
passwordTextField.endEditing(true)
print(passwordTextField.text!)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
phoneAndEmailTextField.endEditing(true)
passwordTextField.endEditing(true)
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField.text != "" {
return true
}else {
return false
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
// sql
}
}
This is are the referenced Outlets of one TextField. The other one is the same... Storyboard
Solution 1:[1]
In your textFieldShouldReturn(_:) method, you reference both phoneAndEmailTextField and passwordTextField. They are both declared as implicitly unwrapped optionals, meaning if either of those outlets is not connected in your storyboard, that function will crash.
Try rewriting that function like this:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
guard let phoneAndEmailTextField = phoneAndEmailTextField else {
print("phoneAndEmailTextField is nil")
return false
}
guard let passwordTextField = passwordTextField else {
print("passwordTextField is nil")
return false
}
phoneAndEmailTextField?.endEditing(true)
passwordTextField?.endEditing(true)
return true
}
(Note that with implicitly unwrapped optionals, if you reference them with a question mark as I did in the edited version of your function, the compiler will still do optional chaining and skip the function call/property reference if the thing being referenced is nil.)
Edit:
If it's the statement phoneAndEmailTextField.delegate = self that's crashing, that tells you that phoneAndEmailTextField is nil. You must be doing something wrong in hooking up your IBOutlets. Add a screenshot of your view controller to your question with the connections inspector shown so we can see the connections of those two outlets.
You could fix the crash by rewriting the line as phoneAndEmailTextField?.delegate = self but the phoneAndEmailTextField outlet will still be nil, so you will only be patching the problem, not fixing it.
BTW, you can connect your text field delegates to your view controller right in your storyboard. Just select the text field, pick the connections inspector, and drag from the delegate connection onto the view controller in the storyboard.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
