ios开发之--通过通知监听textfield的输入状态,判断按钮的状态

第一步:

_rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_rightBtn.frame = CGRectMake(kWidth - -, , kWidth - _tf.frame.size.width-, );
[_rightBtn setBackgroundImage:[UIImage imageNamed:@"send_un"] forState:UIControlStateNormal];
[_rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[_inputView addSubview:_rightBtn];

第二步:

    //监听textfield的输入状态
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidChangeValue:)
name:UITextFieldTextDidChangeNotification
object:_tf];

第三步:

//这里可以通过发送object消息获取注册时指定的UITextField对象
- (void)textFieldDidChangeValue:(NSNotification *)notification
{
UITextField *sender = (UITextField *)[notification object];
if(sender.text.length != )
{
[_rightBtn setBackgroundImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
}else
{
[_rightBtn setBackgroundImage:[UIImage imageNamed:@"send_un"] forState:UIControlStateNormal];
}
}

和简单,仅做记录!

上一篇:QTimer的一些注意事项和探索


下一篇:Objective-C学习笔记(三)——用Objective-C编写第一个程序:Hello,World!