1.效果:
显示数字键盘
显示优化键盘
切换键盘,并修改Return为Done
输出完成后,点击done隐藏键盘
2.主要实现代码:
ViewController中实现接口UITextFieldDelegate
关联控件与变量
注册控件委委托
实现接口方法textFieldShouldReturn以隐藏键盘
如果要实现textfield输入长度控制要实现协议方法shouldChangeCharactersInRange
objectivec
//输入长度限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if(self._textfield == textField){
//输出字符串
NSString *inputstr = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([inputstr length]>16){
UIAlertController *alt = [UIAlertController alertControllerWithTitle:@"提示" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
[alt addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"提示确认");
}]];
[self presentViewController:alt animated:true completion:nil];
return NO;
}
}
return YES;
}
当输入长度超过16后,弹出提示