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

相关推荐
00后程序员张11 小时前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
00后程序员张16 小时前
在 iOS 设备上同时监控 CPU、GPU 与内存的方法
android·ios·小程序·https·uni-app·iphone·webview
TheNextByte116 小时前
如何从锁定的 iPhone 中恢复照片?
ios·iphone
Maynor99618 小时前
Clawdbot手机访问完整教程:像用App一样方便
ios·智能手机·iphone
游戏开发爱好者819 小时前
在 Windows、Linux 与 CI 环境下命令行上传 IPA 到 App Store
linux·windows·ios·ci/cd·小程序·uni-app·iphone
符哥200820 小时前
对比ArkTsUI和Flutter和 SwiftUI 和Jetpack Compose四个框架语法及使用场景。
flutter·ios·swiftui
TheNextByte120 小时前
如何在有或没有备份的 iPhone 上检索已删除的短信
ios·iphone
TheNextByte11 天前
如何在恢复模式下从 iPhone 恢复照片?
ios·cocoa·iphone
TheNextByte11 天前
【已修复】由于软件版本过旧,无法将备份恢复到此 iPhone
ios·cocoa·iphone
2501_916007472 天前
跨平台 App 安全,Flutter、RN、Unity、H5 混合应用加固
android·ios·小程序·https·uni-app·iphone·webview