参考如下代码(下例是控制设置交易密码,控制6位):

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)textFieldDidChange:(NSNotification *)notification
{
    UITextField *textField = (UITextField *)notification.object;
    
    if ( textField.text.length > 6 && textField == _oldTransactionPasswordTextField)
    {
        _oldTransactionPasswordTextField.text = [textField.text substringToIndex:6];
    }
    
    if ( textField.text.length > 6 && textField == _transPasswordTextField)
    {
        _transPasswordTextField.text = [textField.text substringToIndex:6];
    }
}

采用通知做法比delegate中

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string这个方法要好一些(个人觉得)

 

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-06-18
  • 2022-12-23
猜你喜欢
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
相关资源
相似解决方案