MortalMan
MortalMan

Reputation: 2612

Loop user input from text boxes in Swift

I have two text boxes in my iOS to-do app, one for the name of the task and the other for the description. When the user leaves one or both of the text boxes blank, I want to alert the user about the problem and loop this until the two text boxes are no longer blank. Here is the code I have:

var validInput :Bool = false //for while

while (validInput == false) {
    if (txtTask.text == "" || txtDesc.text == "") {
        var alert = UIAlertController(title: "Error", message: "Task and description cannot be blank", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    } else {
        validInput == true
    }
}

This code is inside an @IBAction function which runs when the user presses Done. My code runs in an infinite loop and it is pretty obvious why. How can I achieve what I would like?

I have an idea:

  1. User leaves the text boxes blank and presses done.
  2. An alert pops up warning the user.
  3. Skip the rest of the function, and only run the function again when Done is pressed.

How could I a) put the above into code, or b) use a loop like I have above, properly?

Upvotes: 0

Views: 1394

Answers (4)

FormigaNinja
FormigaNinja

Reputation: 1571

The answer is: Don't do this !

You don't need to loop textFields to watch for value changes. The correct way to do this is using using the UITextField's delegate methods like

- textFieldDidBeginEditing: to know when user did begin editing,

- textField:shouldChangeCharactersInRange:replacementString: when the textField text value changes

- textFieldDidEndEditing: to know when the user end editing

etc...

As described on the docs:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/

Using loops to do this kind of thing is a bad practice in this case. (And you will have to do a lot of stuff to not block the current thread, verify if there is already an Alert on the screen etc)

Upvotes: 1

Andrew Douwes
Andrew Douwes

Reputation: 137

Keep it simple:

    @IBOutlet var textA: UITextField!
    @IBOutlet var textB: UITextField!

    @IBAction func validateButton(sender: AnyObject) {

        if (textA.text == "" || textB.text == "") {

            println("ALERT: BLANK FIELDS")

        } else {

            println("Let's run some code since we're not blank")
        }

    }

Upvotes: 0

Amit89
Amit89

Reputation: 3000

Here no need of while loop. Just do it in a if else loop, because each time you press the Done button after filing text or leaving empty, your piece of code will be executed.

if (txtTask.text == "" || txtDesc.text == "") {
    var alert = UIAlertController(title: "Error", message: "Task and description cannot be blank", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
} else {
      //Do something or print.
  }

I assume this piece of code is in @IBAction method.

Upvotes: 0

Eendje
Eendje

Reputation: 8883

If you use a while loop in your "done" button, it'll be stuck in an infinite loop as you said. The user gets no chance to change anything because of this.
Instead you should use an if statement to check if those boxes are empty and give them a warning if it is, and do nothing.

if condition {
    // Execute your code if both boxes are filled
} else {
    // Show alert
}

If you insists on using a while loop, you'll have to let the user to enter the text in your alerts. Then your code would work.

Upvotes: 0

Related Questions