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

相关推荐
白玉cfc1 小时前
接口与API设计
ios·objective-c
少云清6 小时前
IOS历史版本下载
ios
奶糖的次元空间1 天前
iOS 学习笔记 - SwiftUI 和 简单布局
ios·swift
zhensherlock1 天前
Protocol Launcher 系列:App Store 精准引流与应用推广
javascript·macos·ios·typescript·iphone·mac·ipad
for_ever_love__1 天前
Objective-C学习 NSDictionary,NSMutableDictionary 功能详解
开发语言·学习·ios·objective-c
for_ever_love__1 天前
Objective-C学习 协议和委托
开发语言·学习·ios·objective-c
2501_915909062 天前
不用越狱就看不到 iOS App 内部文件?使用 Keymob 查看和导出应用数据目录
android·ios·小程序·https·uni-app·iphone·webview
@大迁世界2 天前
液态玻璃屏正在侵蚀你的电池
macos·ios·objective-c·cocoa
pop_xiaoli2 天前
【iOS】类与对象底层
macos·ios·objective-c·cocoa·xcode
sp42a2 天前
NativeScript iOS 平台开发技巧
ios·nativescript·app 开发