Question

Swift Programming. Add conditions to code below: 1.) Checks to see if both text fields are...

Swift Programming.

Add conditions to code below:

1.) Checks to see if both text fields are empty and if both are, prints out an error for the user

2.) Checks, if either one of the text fields are empty, also prints out an error for the user

--------------------------------------------------------------------------------------------------------------------------

@IBOutlet weak var addRouting: UITextField! //outlet for the routing textfield
    @IBOutlet weak var addAccount: UITextField! //outlet for the account textfield
  
    @IBAction func saveBankAccount(_ sender: Any) { //button action
      
        if addRouting.text! .isEmpty{ //check for the empty routing
            let alertControl = UIAlertController(title: "ERROR!", message: "Enter Rounting", preferredStyle: .alert)
            alertControl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alertControl, animated: false)
        }
        else if addAccount.text!.isEmpty{ //check for the empty account
            let alertControl = UIAlertController(title: "ERROR!", message: "Enter Account", preferredStyle: .alert)
            alertControl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alertControl, animated: false)
        }
    }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

@IBOutlet weak var addRouting: UITextField!
    @IBOutlet weak var addAccount: UITextField!
  
    @IBAction func alertBox(_ sender: Any) {
        if addRouting.text! .isEmpty && addAccount.text! .isEmpty{ //check for both textbox empty
            let alertControl = UIAlertController(title: "ERROR!", message: "Enter Account and Routing", preferredStyle: .alert)
            alertControl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alertControl, animated: false)
          
        }
        else if addAccount.text!.isEmpty{ //check for the empty account
            let alertControl = UIAlertController(title: "ERROR!", message: "Enter Account", preferredStyle: .alert)
            alertControl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alertControl, animated: false)
        }
        else if addRouting.text! .isEmpty{ //check for the empty routing
            let alertControl = UIAlertController(title: "ERROR!", message: "Enter Rounting", preferredStyle: .alert)
            alertControl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alertControl, animated: false)
        }
    }

Add a comment
Know the answer?
Add Answer to:
Swift Programming. Add conditions to code below: 1.) Checks to see if both text fields are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT