从Swift中的文本框循环用户输入

时间:2022-11-29 16:14:15

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:

我的iOS待办事项应用程序中有两个文本框,一个用于任务名称,另一个用于描述。当用户将一个或两个文本框留空时,我想提醒用户该问题并循环,直到两个文本框不再为空。这是我的代码:

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?

此代码位于@IBAction函数内,该函数在用户按下Done时运行。我的代码在无限循环中运行,这是非常明显的原因。我怎样才能达到我想要的目标?

I have an idea:

我有个主意:

  1. User leaves the text boxes blank and presses done.
  2. 用户将文本框留空并按下完成。

  3. An alert pops up warning the user.
  4. 弹出警告警告用户。

  5. Skip the rest of the function, and only run the function again when Done is pressed.
  6. 跳过功能的其余部分,仅在按下完成时再次运行该功能。

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

我怎么能a)把上面的代码放入代码中,或者b)使用像我上面一样的循环,正确吗?

4 个解决方案

#1


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

您无需循环textFields来监视值更改。正确的方法是使用UITextField的委托方法

- textFieldDidBeginEditing: to know when user did begin editing,

- textFieldDidBeginEditing:知道用户何时开始编辑,

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

- textField:shouldChangeCharactersInRange:replacementString:当textField文本值更改时

- textFieldDidEndEditing: to know when the user end editing

- textFieldDidEndEditing:知道用户何时结束编辑

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)

在这种情况下,使用循环来做这种事情是一种不好的做法。 (并且你必须做很多事情才能阻止当前线程,验证屏幕上是否已经有警报等)

#2


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.

如果你在“完成”按钮中使用while循环,它会像你说的那样陷入无限循环。由于这个原因,用户没有机会改变任何东西。相反,你应该使用if语句检查这些框是否为空,如果是,则给它们一个警告,什么也不做。

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.

如果您坚持使用while循环,则必须让用户在警报中输入文本。那你的代码就行了。

#3


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.

这里不需要while循环。只需在if else循环中执行,因为每次在归档文本或留空后按下完成按钮,您的代码将被执行。

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.

我假设这段代码在@IBAction方法中。

#4


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")
        }

    }

#1


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

您无需循环textFields来监视值更改。正确的方法是使用UITextField的委托方法

- textFieldDidBeginEditing: to know when user did begin editing,

- textFieldDidBeginEditing:知道用户何时开始编辑,

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

- textField:shouldChangeCharactersInRange:replacementString:当textField文本值更改时

- textFieldDidEndEditing: to know when the user end editing

- textFieldDidEndEditing:知道用户何时结束编辑

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)

在这种情况下,使用循环来做这种事情是一种不好的做法。 (并且你必须做很多事情才能阻止当前线程,验证屏幕上是否已经有警报等)

#2


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.

如果你在“完成”按钮中使用while循环,它会像你说的那样陷入无限循环。由于这个原因,用户没有机会改变任何东西。相反,你应该使用if语句检查这些框是否为空,如果是,则给它们一个警告,什么也不做。

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.

如果您坚持使用while循环,则必须让用户在警报中输入文本。那你的代码就行了。

#3


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.

这里不需要while循环。只需在if else循环中执行,因为每次在归档文本或留空后按下完成按钮,您的代码将被执行。

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.

我假设这段代码在@IBAction方法中。

#4


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")
        }

    }