UITextField

--------UITextFieldDelegate-------------

@optional

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.

@end

输入字符限制--限制特殊字符(只让输入数字和字母)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"range:%@----string:%@",[NSValue valueWithRange:range],string);
    const char ch = *[string UTF8String];
    NSLog(@"%c",ch);
    if ((48<=ch&&ch<=57)||(65<=ch&&ch<=90)||(97<=ch&&ch<=122)||string.length==0) {//string.length==0是点击删除键(X键)
        return YES;
    }
    return NO;
}

输入字符个数限制

[_InputUsername addTarget:self action:@selector(RestricCharNumber:) forControlEvents:UIControlEventEditingChanged];

-(void)RestricCharNumber:(UITextField *)sender
{
    if (sender.text.length > 10&&sender.tag==1) {//用户名 限制字符10个
        sender.text = [sender.text substringToIndex:10];
    }
}

容易出现的问题:

//视图控制器 点击退出的时候 该如果返回no 会有问题
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

 

posted @ 2016-08-04 18:06  潜意识  阅读(160)  评论(0编辑  收藏  举报