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

相关推荐
飞Link12 小时前
iOS 27 开启“AI 开放时代”:Siri 驱动可更换背后的技术范式迁移
人工智能·ios
泉木14 小时前
KVC 详解 —— Key-Value Coding 完全指南
ios·swift
sweet丶15 小时前
现有基础上增加设备生物识别登录的一个技术方案
ios
嵌入式×边缘AI:打怪升级日志19 小时前
转换模块(十二):实现 RGB 转 RGB + 项目整合与上机实验
开发语言·ios·swift
唐诺19 小时前
IOS学习路线计划
ios
for_ever_love__19 小时前
UI学习:无限轮播视图
学习·ui·ios·objective-c
MonkeyKing20 小时前
iOS Block 底层深度解析:结构、变量捕获、copy逻辑与循环引用本质
ios
MonkeyKing20 小时前
iOS ARC 本质:__strong / __weak / __unsafe_unretained / __autoreleasing 深度解析
ios
humors22121 小时前
全平台日常使用的国外应用
android·ios·app·安卓·应用·国外