如何检测UITextField的长度

发布于 2024-09-27 00:40:31 字数 324 浏览 2 评论 0原文

我试图获取 UITextField 是否为空,以及是否要在 ibaction 中运行以下代码。

float uu = ([u.text floatValue]);
float ss = ([s.text floatValue]);
float aa = ([a.text floatValue]);


float x1float = sqrt((uu * uu) +(2*aa*ss));


v.text = [[NSString alloc] initWithFormat:@"%f", x1float];

其中 v.text 是 uitextfield 内的文本

I am trying to get if a UITextField is empty, and if it is to run the following code in an ibaction.

float uu = ([u.text floatValue]);
float ss = ([s.text floatValue]);
float aa = ([a.text floatValue]);


float x1float = sqrt((uu * uu) +(2*aa*ss));


v.text = [[NSString alloc] initWithFormat:@"%f", x1float];

where v.text is the text inside the uitextfield

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

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

发布评论

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

评论(1

薄凉少年不暖心 2024-10-04 00:40:31

我假设您的主要挑战不仅仅是检查 UITextFieldtext 属性是否为空,而是让它在用户键入时执行该检查。要简单地检查文本字段是否为空,您只需执行以下操作:

if (aTextField.text == nil || [aTextField.text length] == 0)

但是,如果您尝试让文本字段在变为空时“自动”执行计算,请执行以下操作:为 < 设置委托代码>UITextField。在更改文本字段中的任何字符之前,会调用委托的 textField:shouldChangeCharactersInRange:replacementString: 方法。在该方法中,执行如下操作:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    [self performSelector:@selector(updateEmptyTextField:) withObject:textField afterDelay:0];
    return YES;
}

您在延迟后执行 updateEmptyTextField,因为 UITextField 尚未应用其委托刚刚批准的更改。然后,在 updateEmptyTextField: 中执行如下操作:

- (void)updateEmptyTextField:(UITextField *)aTextField {
    if (aTextField.text == nil || [aTextField.text length] == 0) {
        // Replace empty text in aTextField
    }
}

注意:首次显示视图时,您可能需要手动运行一次检查,因为 textField:shouldChangeCharactersInRange:replacementString: 获胜在用户开始在文本字段中输入之前不会被调用。

I'm assuming that your main challenge here isn't simply checking whether the text property of a UITextField is empty, but getting it to perform that check as the user types. To simply check whether the text field is empty, you'd just do:

if (aTextField.text == nil || [aTextField.text length] == 0)

However, if you're trying to get the text field to "automatically" perform the calculation whenever it becomes empty, do the following: Set a delegate for the UITextField. The delegate's textField:shouldChangeCharactersInRange:replacementString: method is called before any characters are changed in the text field. In that method, do something like:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    [self performSelector:@selector(updateEmptyTextField:) withObject:textField afterDelay:0];
    return YES;
}

You perform updateEmptyTextField after a delay because the UITextField hasn't yet applied the change that its delegate just approved. Then, in updateEmptyTextField: do something like this:

- (void)updateEmptyTextField:(UITextField *)aTextField {
    if (aTextField.text == nil || [aTextField.text length] == 0) {
        // Replace empty text in aTextField
    }
}

Note: You might need to manually run the check once when your view is first displayed, because textField:shouldChangeCharactersInRange:replacementString: won't get called until the user starts typing in the text field.

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