码迷,mamicode.com
首页 > 其他好文 > 详细

控制TextField的内容长度

时间:2016-10-11 18:43:48      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

参考如下代码(下例是控制设置交易密码,控制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这个方法要好一些(个人觉得)

 

控制TextField的内容长度

标签:

原文地址:http://www.cnblogs.com/shpyoucan/p/5950267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!