textfield 输入之后button变色 监听 (IOS 根据输入值改变button颜色)

第一种是给textField添加Target

账号密码的的textField

    [user addTarget:self action:@selector(textValueChanged) forControlEvents:UIControlEventEditingChanged];

    [pwd addTarget:self action:@selector(textValueChanged) forControlEvents:UIControlEventEditingChanged];

    给这个添加方法 根据输入的账号和密码的都不为空时候 button开个变化

-(void)textValueChanged{

    if (user.text.length != 0 && pwd.text.length != 0) {

        [_landBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

_landBtn.backgroundColor = DEF_RGB_COLOR(30 , 144, 255);


    }else{

        [_landBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

_landBtn.backgroundColor = [UIColor lightGrayColor];

    }

}


第二种是 当在 输入框中输入字符串时,button显示高亮( enabled = yes),输入框清空时,变默认颜色(暗色, enabled = no)


  1. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
  2.       
  3.     if (textField == _rightNameTextFeild)  
  4.     {  
  5.         
  6.         NSInteger length = _rightNameTextFeild.text.length;  
  7.         NSInteger location = range.location;  
  8.           
  9.         if ( length > location )  
  10.         {  
  11.             _landBtn.backgroundColor = [CHUtil  colorWithHexString:@"d6d7d9"];  
  12.         }  
  13.         else  
  14.         {  
  15.             length += string.length;  
  16.             _landBtn.backgroundColor = [CHUtil  colorWithHexString:@"fa4a26"];  
  17.         }  
  18.     }  
  19.      
  20.    return YES;  
  21. }  
对textfield的进行监听 自我感觉 若是不复杂的话 第一种还是比较简单的




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值