如何检测UITextField的长度
我试图获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的主要挑战不仅仅是检查
UITextField
的text
属性是否为空,而是让它在用户键入时执行该检查。要简单地检查文本字段是否为空,您只需执行以下操作:但是,如果您尝试让文本字段在变为空时“自动”执行计算,请执行以下操作:为 < 设置委托代码>UITextField。在更改文本字段中的任何字符之前,会调用委托的
textField:shouldChangeCharactersInRange:replacementString:
方法。在该方法中,执行如下操作:您在延迟后执行
updateEmptyTextField
,因为UITextField
尚未应用其委托刚刚批准的更改。然后,在updateEmptyTextField:
中执行如下操作:注意:首次显示视图时,您可能需要手动运行一次检查,因为
textField:shouldChangeCharactersInRange:replacementString:
获胜在用户开始在文本字段中输入之前不会被调用。I'm assuming that your main challenge here isn't simply checking whether the
text
property of aUITextField
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: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'stextField:shouldChangeCharactersInRange:replacementString:
method is called before any characters are changed in the text field. In that method, do something like:You perform
updateEmptyTextField
after a delay because theUITextField
hasn't yet applied the change that its delegate just approved. Then, inupdateEmptyTextField:
do something like this: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.