监听一个对象的值和熟悉的改变

时间:2021-07-08 16:49:18

    先构造出这个对象,

 _pass = [[PassGuardTextField alloc] initWithFrame:CGRectZero];

    _pass.leftView = view2;

    _pass.leftViewMode = UITextFieldViewModeAlways;

    _pass.placeholder = @"输入登录密码";

    [_pass setM_license:kLicense];

    [_pass setM_isDotDelay:YES];

    [_pass setM_uiapp:[UIApplication sharedApplication]];

    [_pass setM_iMaxLen:20];

    [_pass setKeyboardType:UIKeyboardTypeDefault];

    [_pass setM_hasstatus:YES];

    [_pass setM_bsupportrotate:NO];

    [_pass setM_isResignFirstRes:YES];

    [_pass setM_ikeyordertype:KEY_NONE_CHAOS];

    [_pass setM_ikeypresstype:KEY_IPAD_KEY_PRESS | KEY_IPHONE_KEY_PRESS];

    [_pass set_DoneDelegate:self];

    [_inputView addSubview:_pass];



//通过@"text"  注册这个模式

 [_pass addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];



//这个方法就是来监听 text的事件   输入了多少位,

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    if (_pass.text.length >= 6 && _account.text.length >= 6)

        [_loginButton setEnabled:YES];

    else

        [_loginButton setEnabled:NO];

}


//离开界面的时候  删除观察者


    [_pass removeObserver:self forKeyPath:@"text" context:nil];