对UITextField内容实时监听长度和内容
来源:互联网 发布:天津市网络技术研究所 编辑:程序博客网 时间:2024/05/12 09:12
在UISearchBar中,当输入信息改变时,它就会调用textDidChange函数,但是UITextField没有这个功能,唯一与这个类似的
shouldChangeCharactersInRange函数,也是在文件还没有改变前就调用了,而不是在改变后调用,要想实现这个功能,我们可以增加事件监听的方式,这个与java的listener类似.先来看看objective-c提供的接口:
// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
// the action cannot be NULL.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
怎么去使用这个接口呢?主要分为两步,第一步就是在UItextField组件中增加对文件编辑改变时事件的监听,然后再实现监听器监听到事件时,所调用的方法.
//第一步,对组件增加监听器
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
...
//第二步,实现回调函数
- (void) textFieldDidChange:(id) sender {
UITextField *_field = (UITextField *)sender;
NSLog(@"%@,%d",[_field text],_field.text.length);
}
shouldChangeCharactersInRange函数,也是在文件还没有改变前就调用了,而不是在改变后调用,要想实现这个功能,我们可以增加事件监听的方式,这个与java的listener类似.先来看看objective-c提供的接口:
// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
// the action cannot be NULL.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
怎么去使用这个接口呢?主要分为两步,第一步就是在UItextField组件中增加对文件编辑改变时事件的监听,然后再实现监听器监听到事件时,所调用的方法.
//第一步,对组件增加监听器
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
...
//第二步,实现回调函数
- (void) textFieldDidChange:(id) sender {
UITextField *_field = (UITextField *)sender;
NSLog(@"%@,%d",[_field text],_field.text.length);
}
0 0
- 对UITextField内容实时监听长度和内容
- 实时监听UITextField的输入内容变化
- iOS之UITextField对输入的内容的长度限制和内容限制
- iOS开发 实时监听UITextField内容的变化
- iOS实时检测UITextField内容
- iOS 监听UITextfield输入内容
- iOS UITextField实时监听获取输入内容,中文状态去除预输入拼音
- 监听input内容实时变化
- 监听EditText的内容长度
- ios UITextField UITextView 限制内容长度
- 监听UITextField输入内容的简单实现
- 监听UITextField内容改变简单方法
- iOS-UITextField监听输入内容的解决办法
- JS实时检测文本框内容长度
- input输入框内容变化实时监听
- 实时监听输入框的内容
- UITextField UITextView对内容字数的限制
- UITextField 实时监听
- HDU 5093 Battle ships
- CSS3动画遮罩文字特效
- vim配置及插件安装管理
- iOS crash 崩溃问题的追踪方法
- 树状数组学习(一维)
- 对UITextField内容实时监听长度和内容
- 053第495题
- socket通信网络模型
- Apple Mach-O Linker Warning 警告解决办法
- Java Console乱码
- 解决一个页面多个window.onload问题
- 【ASC44】F - Funny Card Game
- Poco TcpServer 介绍
- Qt跨平台的一个例程