iOS隐藏TextFiled控件输入键盘

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后,弹出提示

相关推荐
2501_9159090613 小时前
Charles 抓不到包怎么办?iOS 调试过程中如何判断请求路径
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074713 小时前
iOS和iPadOS文件管理系统全面解析与使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214318 小时前
iOS App 开发阶段性能优化,观察 CPU、内存和日志变化
android·ios·性能优化·小程序·uni-app·iphone·webview
游戏开发爱好者819 小时前
在 iOS 开发、测试与上架过程中 如何做证书管理
android·ios·小程序·https·uni-app·iphone·webview
ii_best19 小时前
按键精灵安卓/IOS手机助手 × 手机按键 App:1 分钟搞定设备连接(超详细教程)
android·ios·智能手机·自动化·编辑器
2501_9160074720 小时前
在没有 Mac 的情况下完成 iOS 应用上架 App Store
android·macos·ios·小程序·uni-app·iphone·webview
TheNextByte120 小时前
iPhone存储空间已满?如何轻松释放iPhone空间?
android·ios·iphone
—Qeyser1 天前
Flutter 颜色完全指南
android·flutter·ios
2501_916008891 天前
iOS 上架需要哪些准备,账号、Bundle ID、证书、描述文件、安装测试及上传
android·ios·小程序·https·uni-app·iphone·webview
Zender Han2 天前
Flutter Android 启动页 & App 图标替换(不使用任何插件的完整实践)
android·flutter·ios