Reputation: 150
I'm having trouble reading the values from a textfield. Every time I type in this line let username = UsernameTextField.text
I get an error saying "Cannot use instance member 'UsernameTextField' within property initializer; property initializers run before 'self' is available".
Here is the full code:
import UIKit
class SignInViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!
@IBOutlet var LogInButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
navigationController?.view.backgroundColor = UIColor.clear
// Remove Autocorrection Type
UsernameTextField.autocorrectionType = .no
PasswordTextField.autocorrectionType = .no
PasswordTextField.textContentType = UITextContentType("")
//Next button takes user to the next textfield
UsernameTextField.delegate = self
PasswordTextField.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UsernameTextField{
PasswordTextField.becomeFirstResponder()
} else if textField == PasswordTextField {
self.view.endEditing(true)
}
return true
}
let username = UsernameTextField.text
}
Upvotes: 0
Views: 68
Reputation:
You have to move you constants and place them right under "return true"
Upvotes: 0
Reputation: 61
Any chance that you have a subclass of UITextField
called UsernameTextField
?
If so, it might be referencing the class instead of the instance.
I would recommend keeping variables' names camelCased. Makes it easier to differentiate between variables and classes.
If this is the convention you use, you might also try using
let name = self.UsernameTextField.text
EDIT:
It seems that this line is outside the function scope.
Should move it before the closing }
.
Upvotes: 1
Reputation: 350
As the error suggests "UsernameTextField" is an instance member which means it does not get instantiated until the class is instantiated (as opposed to static members) which means that by the time your line is compiled which is before the class is instantiated, that member does not exist yet.
Upvotes: 0
Reputation: 2216
That is what you have, the call to set the text is NOT in a function.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UsernameTextField{
PasswordTextField.becomeFirstResponder()
} else if textField == PasswordTextField {
self.view.endEditing(true)
}
return true
}
let username = UsernameTextField.text // THIS IS NOT IN A FUNCTION
Move it here:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UsernameTextField{
PasswordTextField.becomeFirstResponder()
} else if textField == PasswordTextField {
self.view.endEditing(true)
}
let username = UsernameTextField.text // THIS IS NOW IN A FUNCTION
return true
}
Upvotes: 1
Reputation: 1496
The line
let username = UsernameTextField.text
is in the scope of your class definition, not inside the scope of a method. Therefore this constitutes a property initialization which is run upon construction of an instance of the class. You're trying to initialize the member variable username using ANOTHER member variable (UsernameTextField). This, of course, cannot work, since the object has to be fully initialized before any members can be read.
Did you mean to put the line inside the scope of the method textFieldShouldReturn?
Upvotes: 0