要启用按钮,如果所有的文本框被填充的ios [英] To enable button if all the textfields are filled in ios

查看:111
本文介绍了要启用按钮,如果所有的文本框被填充的ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设计了一个注册表格在iOS中有四个的UITextField S和的UIButton 。我已经设置了按钮启用属性 NO 默认情况下。现在,我想启用,只有当所有四个文本框填充的按钮。你能帮帮我这个作为我一个新的到iOS,坚持了这个问题。

I have designed a signup form in ios with four UITextFields and a UIButton. I have set the buttons' enabled property to NO by default. Now I want to enable the button only when all the four textfields are filled. Can you please help me out with this as I a new to ios, and stuck with this issue.

推荐答案

一个更好的办法是使用像 didChange 方法 UITextViewDelegate 方法,但我们知道 UITextFieldDelegate 不具有 didChange 方法。您可以手动添加行为。您可以使用 shouldChangeCharactersInRange:方法,但我个人会建议不要覆盖方法,除非你绝对必须的。

A better way would be to use the didChange method like the UITextViewDelegate method, but as we know the UITextFieldDelegate does not have a didChange method. You can manually add behaviour. You can use the shouldChangeCharactersInRange: method, but personally I would advise not to override methods unless you absolutely have to.

您可以使用添加行为:

[myTextField1 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];//And so on for all your text fields

和目标的方法:

- (void)textFieldDidChange:(UITextField*)textField{

    if (myTextField1.text.length > 0 && myTextField2.text.length > 0 && myTextField3.text.length > 0 && myTextField4.text.length > 0){

        myButton.enabled = YES;

    } else {

        myButton.enabled = NO;
    }
}

编辑:
此外,如果你想确保他们启用的只有的,如果用户输入有效的文本,而不是空的空间,可以使用以下方法来得到修整文本,检查是否该修剪的文本长度为> 0:

Further, if you want to make sure they are enabled only if the user has entered valid text, and not empty spaces, you may use the following to get the trimmed text, check if this trimmed text has length > 0:

NSUInteger textLength = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

这篇关于要启用按钮,如果所有的文本框被填充的ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆