从 Swift 中的文本框循环用户输入
Loop user input from text boxes in Swift
我的 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
}
}
此代码位于 @IBAction
函数内,当用户按下 Done
时,该函数 运行 就会执行。我的代码 运行 处于无限循环中,原因很明显。我怎样才能实现我想要的?
我有一个想法:
- 用户将文本框留空并按完成。
- 弹出警告警告用户。
- 跳过其余的功能,只有在按下
Done
时才再次运行该功能。
我怎样才能 a) 将上面的代码放入代码中,或者 b) 正确地使用上面的循环?
如果您在 "done" 按钮中使用 while
循环,它将像您所说的那样陷入无限循环。因此,用户没有机会更改任何内容。
相反,您应该使用 if
语句来检查这些框是否为空,如果是,则发出警告,什么也不做。
if condition {
// Execute your code if both boxes are filled
} else {
// Show alert
}
如果您坚持使用 while 循环,则必须让用户在您的提醒中输入文本。然后你的代码就可以工作了。
这里不需要while循环。只需在 if else 循环中执行即可,因为每次您在填写文本或留空后按下 Done
按钮时,您的代码片段都会被执行。
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.
}
我假设这段代码在 @IBAction
方法中。
保持简单:
@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")
}
}
答案是:不要这样做!
您不需要循环 textFields 来观察值的变化。正确的方法是使用 UITextField 的委托方法,例如
- textFieldDidBeginEditing:
了解用户何时开始编辑,
- textField:shouldChangeCharactersInRange:replacementString:
当 textField 文本值改变时
- textFieldDidEndEditing:
知道用户何时结束编辑
等...
如文档所述:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/
在这种情况下,使用循环来做这种事情是一种不好的做法。 (而且你必须做很多事情才能不阻塞当前线程,验证屏幕上是否已经有警报等)
我的 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
}
}
此代码位于 @IBAction
函数内,当用户按下 Done
时,该函数 运行 就会执行。我的代码 运行 处于无限循环中,原因很明显。我怎样才能实现我想要的?
我有一个想法:
- 用户将文本框留空并按完成。
- 弹出警告警告用户。
- 跳过其余的功能,只有在按下
Done
时才再次运行该功能。
我怎样才能 a) 将上面的代码放入代码中,或者 b) 正确地使用上面的循环?
如果您在 "done" 按钮中使用 while
循环,它将像您所说的那样陷入无限循环。因此,用户没有机会更改任何内容。
相反,您应该使用 if
语句来检查这些框是否为空,如果是,则发出警告,什么也不做。
if condition {
// Execute your code if both boxes are filled
} else {
// Show alert
}
如果您坚持使用 while 循环,则必须让用户在您的提醒中输入文本。然后你的代码就可以工作了。
这里不需要while循环。只需在 if else 循环中执行即可,因为每次您在填写文本或留空后按下 Done
按钮时,您的代码片段都会被执行。
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.
}
我假设这段代码在 @IBAction
方法中。
保持简单:
@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")
}
}
答案是:不要这样做!
您不需要循环 textFields 来观察值的变化。正确的方法是使用 UITextField 的委托方法,例如
- textFieldDidBeginEditing:
了解用户何时开始编辑,
- textField:shouldChangeCharactersInRange:replacementString:
当 textField 文本值改变时
- textFieldDidEndEditing:
知道用户何时结束编辑
等...
如文档所述:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/
在这种情况下,使用循环来做这种事情是一种不好的做法。 (而且你必须做很多事情才能不阻塞当前线程,验证屏幕上是否已经有警报等)