简体   繁体   English

如何测试UITextField为零?

[英]How to test a UITextField for being nil?

I am trying to make a part of my app where if the person doesn't change the blank text in my UITextField, then he/she can't go on to the next step. 我正在尝试创建我的应用程序的一部分,如果该人没有更改我的UITextField中的空白文本,那么他/她不能继续下一步。 Basically, I want to test the UITextField for nil text. 基本上,我想测试UITextField的nil文本。 I have used the if (text == @"") method, but if the person clicks on the UITextField but doesn't type, then the if statement doesn't work. 我使用了if(text == @“”)方法,但如果用户单击UITextField但未键入,则if语句不起作用。 For some reason it doesn't think the text == nil or "". 由于某种原因,它不认为文本== nil或“”。 Am I implementing the code wrong. 我是否实施了错误的代码。 Any other options. 任何其他选择。 Please help!!! 请帮忙!!!

You should be checking the length of the text property: 您应该检查text属性的length

if([[textField text] length] == 0) {
  //do something...
}

Here's the category I use... 这是我使用的类别......

@implementation NSString (NSString+Extensions)            
- (BOOL)isNotBlank {
    return [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0;
}        
@end

This way a nil string would evaluate to false, which is correct. 这样一个nil字符串将评估为false,这是正确的。 Creating an isBlank would return false for nil, which isn't correct. 创建isBlank将为nil返回false,这是不正确的。

I have write code to check the string is empty or not. 我编写代码来检查字符串是否为空。 This code also check for the string only space that is also empty for store name and address etc. this will help you. 此代码还会检查字符串唯一的空间,该空间对于商店名称和地址等也是空的。这将对您有所帮助。

NSString *stringTemp = textField.text;
stringTemp = [stringTemp stringByReplacingOccurrencesOfString:@" " withString:@""];

if ([stringTemp isEqualToString:@""]) {
    NSLog(@"Empty string");
}
else{
    NSLog(@"string has some content ");
}

Thanks 谢谢

If I were you I would disable and enable the button while the user is typing. 如果我是你,我会在用户输入时禁用并启用该按钮。 Imho it's better that the button looks disabled when there is no text than having the user click the button to tell him that he is not allowed to move to the next view. Imho最好是在没有文本时按钮看起来禁用,而不是让用户点击按钮告诉他不允许他移动到下一个视图。 Most of apples own apps do it like this. 大多数苹果自己的应用都是这样做的。

You achieve this behavior by using the UITextFieldDelegate method like this 您可以使用像这样的UITextFieldDelegate方法来实现此行为

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // "Length of existing text" - "Length of replaced text" + "Length of replacement text"
    NSInteger textLength = [aTextView.text length] - range.length + [text length];

    if (textLength > 0) {
        doneButton.enabled = YES;
    }
    else {
        doneButton.enabled = NO;
    }
    return YES;
}

If you provide a prefilled textfield you have to enable the button in viewDidLoad (or where ever you want) and if you provide an empty field you have to disable it initally. 如果您提供预填充的文本字段,则必须在viewDidLoad中启用该按钮(或者您想要的任何地方),如果您提供空字段,则必须首先禁用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM