如何测试 UITextField 是否为零?

发布于 2024-11-01 06:47:03 字数 212 浏览 1 评论 0原文

我正在尝试制作我的应用程序的一部分,如果该人不更改我的 UITextField 中的空白文本,那么他/她将无法继续下一步。基本上,我想测试 UITextField 的 nil 文本。我已经使用了 if (text == @"") 方法,但是如果用户单击 UITextField 但没有键入,则 if 语句不起作用。由于某种原因,它不认为文本 == 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. Basically, I want to test the UITextField for nil text. 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. For some reason it doesn't think the text == nil or "". Am I implementing the code wrong. Any other options. Please help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仙气飘飘 2024-11-08 06:47:03

您应该检查 text 属性的 length

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

You should be checking the length of the text property:

if([[textField text] length] == 0) {
  //do something...
}
一场春暖 2024-11-08 06:47:03

这是我使用的类别...

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

这样一个 nil 字符串将计算为 false,这是正确的。创建 isBlank 会返回 false 表示 nil,这是不正确的。

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. Creating an isBlank would return false for nil, which isn't correct.

洋洋洒洒 2024-11-08 06:47:03

我已经编写代码来检查字符串是否为空。此代码还检查仅字符串空间,该空间对于商店名称和地址等也为空。这将对您有所帮助。

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

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

谢谢

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

不必了 2024-11-08 06:47:03

如果我是你,我会在用户打字时禁用和启用该按钮。恕我直言,当没有文本时按钮看起来被禁用比让用户单击按钮告诉他不允许他移动到下一个视图要好。大多数苹果自己的应用程序都是这样做的。

您可以通过使用 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;
}

如果您提供预填充文本字段,您必须在 viewDidLoad 中(或任何您想要的地方)启用按钮,如果您提供空字段,则必须首先禁用它。

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. Most of apples own apps do it like this.

You achieve this behavior by using the UITextFieldDelegate method like this

- (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文