监测UITextField的变化
2016-06-24 11:04
323 查看
监测UITextField的变化可以为UIControlEventEditingChanged事件添加target。
我们有时候会需要用到这个需求:输入框输入文本超过xx长度,不再允许输入其他内容!
UITextField 代理方法本身是无法满足这个需求的。(当然你可以给UITextView添加placeholder实现相同的需求,此处不做介绍。)
示例代码如下:
参考资料:
传送门一(Swift)
传送门二(OC)
我们有时候会需要用到这个需求:输入框输入文本超过xx长度,不再允许输入其他内容!
UITextField 代理方法本身是无法满足这个需求的。(当然你可以给UITextView添加placeholder实现相同的需求,此处不做介绍。)
示例代码如下:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20.f, 100.f, CGRectGetWidth(self.view.frame) - 40.f, 30.f)]; textField.backgroundColor = [UIColor whiteColor]; textField.placeholder = @"placeholder_King"; [self.view addSubview:textField]; // 添加 UIControlEventEditingChanged target事件,即可实时监测textField 内容,并做一些操作 [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (void)textFieldDidChange:(UITextField *)textField { if (textField == self.textField) { if (textField.text.length > 40) { textField.text = [textField.text substringToIndex:40]; } } }
参考资料:
传送门一(Swift)
传送门二(OC)
相关文章推荐
- 关于UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的问题
- easyui只显示年月
- git clone的错误fatal: HTTP request failed解决办法
- UIAutomation使用简要介绍
- 设计模式之builder模式
- UI控件之Date & Time组件(上)
- UI细节点滴-UIScorllView和UITableView/UITextView拖动退出键盘方法
- SQL Queries from Transactional Plugin Pipeline
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
- easyui的基本用法
- SSIS Passing Parameters to an ADO .NET Source query;向ado.net数据源传递参数。
- Color types not allowed (at ‘drawable’ with value ‘#f0f0f0’)
- UIsheji
- UIsheji
- UiSelector的官方帮助文档
- NGUI自适应
- 实例讲解iOS中的UIPageViewController翻页视图控制器
- iOS UIFont 字体名字大全
- EasyUI在表单提交之前进行验证的实例代码
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue