UITextField为空时的值是多少?我似乎无法做到这一点。
我试过了(其中`phraseBox'是所说的UITextField的名字
if(phraseBox.text != @""){
和
if(phraseBox.text != nil){
我错过了什么?
答案 0 :(得分:21)
// Check to see if it's blank
if([phraseBox.text isEqualToString:@""]) {
// There's no text in the box.
}
// Check to see if it's NOT blank
if(![phraseBox.text isEqualToString:@""]) {
// There's text in the box.
}
答案 1 :(得分:13)
在搜索同样的东西时在苹果讨论中找到了这个,想到这里也发布了不好意思。 检查字符串的长度:
NSString *value = textField.text;
if([value length] == 0) {
}
或者在验证之前可以选择修剪它的空格,这样用户就不能输入空格了。对于用户名来说很好。
NSString *value = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if([value length] == 0) {
// Alert the user they forgot something
}
答案 2 :(得分:1)
尝试以下代码
textField.text是一个字符串值,所以我们像这样检查它
if([txtPhraseBox.text isEqualToString:@""])
{
// There's no text in the box.
}
else
{
NSLog(@"Text Field Text == : %@ ",txtPhraseBox.text);
}
答案 3 :(得分:1)
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *fullText = [textField.text stringByAppendingString:string];
if ((range.location == 0) && [self isABackSpace:string]) {
//the textFiled will be empty
}
return YES;
}
-(BOOL)isABackSpace:(NSString*)string {
NSString* check =@"Check";
check = [check stringByAppendingString:string];
if ([check isEqualToString:@"Check"]) {
return YES;
}
return NO;
}
答案 4 :(得分:0)
实际上,我使用Raphael的方法遇到了多个文本字段的问题。这就是我想出的:
if ((usernameTextField.text.length > 0) && (passwordTextField.text.length > 0)) {
loginButton.enabled = YES;
} else {
loginButton.enabled = NO;
}
答案 5 :(得分:0)
用于文本字段验证:
-(BOOL)validation{
if ([emailtextfield.text length] <= 0) {
[UIAlertView showAlertViewWithTitle:AlertTitle message:AlertWhenemailblank];
return NO; }
return YES;}
答案 6 :(得分:0)
验证空UITextfield。如果您不希望UITextField不接受空白空格。使用此代码段:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound) {
return YES;
} else {
return NO;
}
}