ios開發之--通過通知監聽textfield的輸入狀態,判斷按鈕的狀態


第一步:

_rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.frame = CGRectMake(kWidth - 50-5, 7, kWidth - _tf.frame.size.width-10, 45);
    [_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 != 0)
    {
        [_rightBtn setBackgroundImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
    }else
    {
        [_rightBtn setBackgroundImage:[UIImage imageNamed:@"send_un"] forState:UIControlStateNormal];
    }
}

和簡單,僅做記錄!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM