In an app I am developing, there is an email address and password UITextField.
I am trying to set conditions so that if either or both are empty ("") when a SignIn
button is pressed, then the placeholder text should be red, highlighting to the user to complete them.
I am very new to iOS development (Or development in general) so my logical thinking is probably wrong. Anyway, here is what I wrote and started with:
@IBAction func signInTapped(_ sender: Any) {
if emailField.text == "" {
emailField.attributedPlaceholder = NSAttributedString(string: "Email address", attributes: [NSForegroundColorAttributeName: UIColor.red])
if pwdField.text == "" {
pwdField.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSForegroundColorAttributeName: UIColor.red])
}
}else {
This works perfectly if:
- Both fields are empty
- Email address is empty and password field is filled
But... if the email address field is filled and the password field is empty, the password field placeholder text does not change.
I would love to know where I'm going wrong or if there's a more simpler/logical way to achieve the result.
I don't like swift's way with {}
so for the example I am showing them different.
Your code with different indents:
@IBAction func signInTapped(_ sender: Any)
{
if emailField.text == ""
{
emailField.attributedPlaceholder = NSAttributedString(string: "Email
address", attributes: [NSForegroundColorAttributeName:
UIColor.red])
if pwdField.text == ""
{
pwdField.attributedPlaceholder = NSAttributedString(string:
"Password", attributes: [NSForegroundColorAttributeName:
UIColor.red])
}
}
else {
Notice how your if
statements are nested. The pwdField
will not be checked unless the emailField
is empty.
To fix it unstack them and note that I moved the else
and turned it to a else if
,
Fixed Code:
@IBAction func signInTapped(_ sender: Any)
{
if emailField.text == ""
{
emailField.attributedPlaceholder = NSAttributedString(string: "Email
address", attributes: [NSForegroundColorAttributeName:
UIColor.red])
}
if pwdField == ""
{
pwdField.attributedPlaceholder = NSAttributedString(string:
"Password", attributes: [NSForegroundColorAttributeName:
UIColor.red])
}
else if emailField.text != ""
{
//here both fields have text inside them
}
}
You have an If statement inside another if statement.
Instead of this:
if emailField.text == "" {
emailField.attributedPlaceholder = NSAttributedString(string: "Email address", attributes: [NSForegroundColorAttributeName: UIColor.red])
if pwdField.text == "" {
pwdField.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSForegroundColorAttributeName: UIColor.red])
}
}
Use this:
if emailField.text == "" {
emailField.attributedPlaceholder = NSAttributedString(string: "Email address", attributes: [NSForegroundColorAttributeName: UIColor.red])
}
if pwdField.text == "" {
pwdField.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSForegroundColorAttributeName: UIColor.red])
}
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.