xcode 如何检查 UITextFields 是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9015969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if UITextFields are empty?
提问by Teddy13
I have a bunch of UITextFields where a user must enter a number (the keyboard is the number pad) before pressing a submit button. 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....
我有一堆 UITextFields,用户必须在按下提交按钮之前输入一个数字(键盘是数字键盘)。当提交按钮被按下时,会调用一个“检查”方法来检查数据是否有效......也就是不为空。现在这很容易,如果它只是一个 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?
我将如何制作伪代码 if 语句如下所示?
Thanks
谢谢
if (!valid)
{
NSLog (@"not valid)
}
else
{
//proceed to next method
}
回答by Assad Ullah
I am supposing UITextField variable as *tfieldso here is the solution
我假设 UITextField 变量为*tfield所以这里是解决方案
if (tfield.text.length > 0 || tfield.text != nil || ![tfield.text isEqual:@""])
{
//do your work
}
else
{
//through error
}
回答by AmirZ
or you could just call
或者你可以打电话
[myTextField hasText];
which will return NO if the field is empty.
如果该字段为空,它将返回 NO。
回答by mattjgalloway
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. For that, take a look at NSString
's stringByTrimmingCharactersInSet:
method like so:
您可能希望在抓取时修剪空白字符,temp1
以便它@" "
也是空白的。为此,请查看NSString
'sstringByTrimmingCharactersInSet:
方法,如下所示:
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
}
回答by ksh
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
}
}];
回答by Chander Shakher Ghorela - Guru
you can put like this
if(![temp1 isEqualToString:@""] && ![temp2 isEqualToString:@""] ... ![temp14 isEqualToString:@""])
{
NSLog (@" valid);
}
else
{NSLog (@"not valid);
}
回答by Vikash Sinha
In Swift,
在斯威夫特,
You can use this block of code.
您可以使用此代码块。
if textField.text?.isEmpty {
//Return true if the textfield is empty or false if it's not empty.
}