如何限制文本框輸入的個數
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
textF.backgroundColor = [UIColor grayColor];
[self.view addSubview:textF];
self.textF = textF;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextField:) name:@"UITextFieldTextDidChangeNotification" object:textF];
}
- (void)limitTextField:(NSNotification *)note {
int limit = 5;
if ([self.textF.text length] > limit) {
[self.textF setText:[self.textF.text substringToIndex:limit]];
}
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
[super viewDidLoad];
UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
textF.backgroundColor = [UIColor grayColor];
[self.view addSubview:textF];
self.textF = textF;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextField:) name:@"UITextFieldTextDidChangeNotification" object:textF];
}
- (void)limitTextField:(NSNotification *)note {
int limit = 5;
if ([self.textF.text length] > limit) {
[self.textF setText:[self.textF.text substringToIndex:limit]];
}
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.