Ios 如何交替检查swift中的所有uitextfields?
我是swift编程语言的初学者。我正在寻找检查所有文本字段的替代方法。我想做的是。若文本字段并没有任何值,则将文本字段文本更改为红色文本字段名称,并向用户发送警报。如果用户开始键入chance文本,请将其颜色恢复为黑色 这是我的方法,但我认为这不是正确的方法Ios 如何交替检查swift中的所有uitextfields?,ios,swift,Ios,Swift,我是swift编程语言的初学者。我正在寻找检查所有文本字段的替代方法。我想做的是。若文本字段并没有任何值,则将文本字段文本更改为红色文本字段名称,并向用户发送警报。如果用户开始键入chance文本,请将其颜色恢复为黑色 这是我的方法,但我认为这不是正确的方法 func alertMessage(title:String, message:String)->Void{ var alert = UIAlertController(title: title, message: mess
func alertMessage(title:String, message:String)->Void{
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
func checkAllFields()->Void{
var allFields=[String:String]()
var fieldCounter = 0
allFields = ["Username":userNameField.text,"Name":nameField.text,"Surname":surNameField.text,"Email":emailField.text, "Password":passwordField.text]
for (field, fieldData) in allFields{
if fieldData.isEmpty{
fieldCounter++
}
}
if fieldCounter <= allFields.count{
var title = "Missing Fields"
var message = "You need to fill all fields."
if userNameField.text.isEmpty{
userNameField.textColor = UIColor.redColor()
userNameField.text = "Username"
}
if nameField.text.isEmpty{
nameField.textColor = UIColor.redColor()
nameField.text = "Name"
}
if surNameField.text.isEmpty{
surNameField.textColor = UIColor.redColor()
surNameField.text = "Surname"
}
if emailField.text.isEmpty {
emailField.textColor = UIColor.redColor()
emailField.text = "Email"
}
if passwordField.text.isEmpty {
passwordField.textColor = UIColor.redColor()
passwordField.text = "Password"
}
if retypePasswordField.text.isEmpty{
retypePasswordField.textColor = UIColor.redColor()
retypePasswordField.text = "Retype Password"
}
alertMessage(title, message: message)
}
}
func alertMessage(标题:String,消息:String)->Void{
var alert=UIAlertController(标题:标题,消息:消息,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Default,处理程序:nil))
self.presentViewController(警报、动画:true、完成:nil)
}
func checkAllFields()->Void{
var allFields=[String:String]()
变量字段计数器=0
allFields=[“用户名”:userNameField.text,“名称”:nameField.text,“姓氏”:nameField.text,“电子邮件”:emailField.text,“密码”:passwordField.text]
用于所有字段中的(字段,字段数据){
如果fieldData.i为空{
现场计数器++
}
}
如果fieldCounter我建议通过获取UITextView而不是其内容来简化代码:
func checkAllFields()->Void{
var allFields=[String:UITextView]()
var fieldCounter = 0
allFields = ["Username":userNameField, "Name":nameField, "Surname":surNameField, "Email":emailField, "Password":passwordField]
for (fieldName, fieldTextView) in allFields{
if fieldTextView.text.isEmpty{
fieldTextView.textColor = UIColor.redColor()
fieldTextView.text = fieldName
fieldCounter++
}
}
if fieldCounter > 0 {
var title = "Missing Fields"
var message = "You need to fill all fields."
alertMessage(title, message: message)
}