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

相关推荐
牛马1115 小时前
iOS :Codable协议,字典,数组的详细分析和比较
ios
TheNextByte110 小时前
如何将 iPhone 备份到外置硬盘?
ios·iphone
佛系打工仔11 小时前
绘制K线第三章:拖拽功能实现
android·前端·ios
游戏开发爱好者814 小时前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
HH思️️无邪15 小时前
Flutter 项目 -从 0 到 1 实现 iOS WatchApp
flutter·ios
TheNextByte116 小时前
如何将 iPhone 上的联系人备份到 iCloud?
ios·iphone·icloud
牛马11116 小时前
iOS swift 自定义View
ios·cocoa·swift
2501_9151063217 小时前
HBuilderX 项目上架 iOS app上架 App Store 的关键流程
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063217 小时前
iOS 文件管理,在不越狱的前提下管理 iPhone / iPad 文件
android·ios·小程序·uni-app·iphone·webview·ipad
牛马11117 小时前
ios swift处理json数据
ios·json·swift