Linked Questions

Popular Questions

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.

Related Questions