厚积薄发,滴水穿石

搬家到主站了:http://www.cnblogs.com/cokecoffe/
随笔 - 45, 文章 - 8, 评论 - 12, 引用 - 0
数据加载中……

UITextField增加textDidChange回调功能(转)

在UISearchBar中,当输入信息改变时,它就会调用textDidChange函数,但是UITextField没有这个功能,唯一与这个类似的shouldChangeCharactersInRange函数,
也是在文件还没有改变前就调用了,而不是在改变后调用,要想实现这个功能,我们可以增加事件监听的方式,这个与java的listener类似.先来看看objective-c提供的接口:
1 // add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event. 
2 // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order 
3 // the action cannot be NULL. 
4 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

怎么去使用这个接口呢?主要分为两步,第一步就是在UItextField组件中增加对文件编辑改变时事件的监听,然后再实现监听器监听到事件时,所调用的方法.:
1 //第一步,对组件增加监听器 
2 [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 
3
4 //第二步,实现回调函数 - (void) textFieldDidChange:(id) sender 
5 
6     UITextField *_field = (UITextField *)sender;     
7         NSLog(@"%@",[_field text]); 
8 }

http://www.voland.com.cn/uitextfield_increase_textdidchange_callback_function

posted on 2012-02-02 13:10 Wangkeke 阅读(1279) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理