开发中很多这种情况,就是页面中有一些需要填写的信息,然后有一个按钮,当页面中的textfeild 还未填写完整时,按钮是不可点击的状态,填写完整后才可以点击,该怎么实现呢,实现思路:
因为页面中先填写哪个textfield是不一定的,所以我需要给每个textfeild 添加监听事件,当编辑状态改变时,去判断页面中所有的textfeild 是否都已经有值,有值则改变按钮的可点击状态
由于项目中可能有很多页面都会用到判断的方法,所以我们可以把方法封装起来,在用到的地方直接调取就行
上代码(创建一个类,封装方法):
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//判断是否有值,只要有no,就为灰色,如果都为yes,那就直到最后一个都为yes时,即为yes
+ (void)setbuttonstatusbytextfeilds:(nsarray *)textfeildarr button:(uibutton *)button{
for (uitextfield *textf in textfeildarr) {
if (textf.text.length <= 0) {
[button setbackgroundcolor:klinecolor];
button.enabled = no;
return;
}else{
if ([(uitextfield *)textfeildarr[textfeildarr.count - 1] text] > 0) {
[button setbackgroundcolor:kgreencolor];
button.enabled = yes;
}
}
}
}
|
在用到的地方:
?| 1 2 3 4 5 |
//给每个textfeild 添加监听
self.textfarr = @[_passordf,_surepasswordf];
for (uitextfield *textf in _textfarr) {
[textf addtarget:self action:@selector(setbtnstatus) forcontrolevents:(uicontroleventeditingchanged)];
}
|
| 1 2 3 4 |
//调取判断方法
- (void)setbtnstatus{
[showtool setbuttonstatusbytextfeilds:_textfarr button:_registerbtn];
}
|
效果图:


以上这篇ios 判断页面中的该填项是否填完整,改变按钮状态的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/Kaiccy/article/details/78932336








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。