UITextField增加textDidChange回调功能
2010-01-20, Posted in Objective-c | 1 回复
在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(@"%@",[_field text]); }
1 Trackback or Pingback for this entry
四月 24th, 2011 on 09:49:44
[...] 原文链接:http://www.voland.com.cn/uitextfield_increase_textdidchange_callback_function >>> 进入[Android2D游戏开发]主题文章列表 转载编辑: Fgamers 转载地址:http://disanji.net/2011/04/24/ios-uitextfield-textdidchange-callback/ 分享到 | blog comments powered by Disqus /* [...]