[英]How to check if UITextFields are empty?
I have a bunch of UITextFields where a user must enter a number (the keyboard is the number pad) before pressing a submit button. 我有一堆UITextFields,用户必须在按下提交按钮之前输入一个数字(键盘是数字键盘)。 When the submit button is pressed, a "check" method is called that wants to check if the data is valid... aka not empty.
当按下提交按钮时,调用“检查”方法,该方法想要检查数据是否有效...也称为空。 Now this would be easy if it was just one textField but I have 14. I want a simple if-statement that checks if one of the fields or all of the fields are empty....
现在这很容易,如果它只是一个textField但我有14.我想要一个简单的if语句来检查其中一个字段或所有字段是否为空....
I have the follow UITextFields declared: number1, number2, number3 etc etc and I have the follow strings declared which can take the value of the UITextField.text... they are declared: temp1, temp2, temp3 etc... 我有跟随UITextFields声明:number1,number2,number3等等,我有声明的后续字符串,它可以取UITextField.text的值...它们被声明:temp1,temp2,temp3等...
how would I go about making a pseudocode if statement like the one below? 我将如何制作一个伪代码如果语句如下所示?
Thanks 谢谢
if (!valid)
{
NSLog (@"not valid)
}
else
{
//proceed to next method
}
I am supposing UITextField variable as *tfield so here is the solution 我假设UITextField变量为* tfield,所以这里是解决方案
if (tfield.text.length > 0 || tfield.text != nil || ![tfield.text isEqual:@""])
{
//do your work
}
else
{
//through error
}
or you could just call 或者你可以打电话
[myTextField hasText];
which will return NO if the field is empty. 如果字段为空,它将返回NO。
I think something like this is what you want. 我觉得这样的事情就是你想要的。 It's just using
isEqualToString:
to check if the string is empty. 它只是使用
isEqualToString:
来检查字符串是否为空。
NSString *temp1 = number1.text;
NSString *temp2 = number2.text;
... ///< temp3, etc
if ([temp1 isEqualToString:@""]) {
// temp1 not valid
} else if ([temp2 isEqualToString:@""]) {
// temp2 not valid
} ... {
// temp3, etc
} else {
// Valid
}
You may want to trim whitespace characters when grabbing temp1
so that @" "
would also be blank. 抓取
temp1
时可能需要修剪空白字符,以便@" "
也为空。 For that, take a look at NSString
's stringByTrimmingCharactersInSet:
method like so: 为此,请查看
NSString
的stringByTrimmingCharactersInSet:
方法,如下所示:
NSString *temp1 = [number1.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Update: 更新:
If you want to do it with an array you could do something like: 如果你想用数组做,你可以做类似的事情:
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
[array addObject:number1.text];
[array addObject:number2.text];
... ///< number3, etc
BOOL ok = YES;
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isEqualToString:@""]) {
ok = NO;
*stop = YES;
}
}];
if (ok) {
// Valid
} else {
// Not valid
}
Something like this would iterate through your UITextFields: 像这样的东西会迭代你的UITextFields:
[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UITextField class]]) {
// do your checks
}
}];
you can put like this
if(![temp1 isEqualToString:@""] && ![temp2 isEqualToString:@""] ... ![temp14 isEqualToString:@""])
{
NSLog (@" valid);
}
else
{NSLog (@"not valid);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.