From: http://wuchaorang.2008.blog.163.com/blog/static/48891852201402184156861/
1.UITextField的UIControlEventValueChanged事件单独注册,你会发现根本不执行,需要多注册一个通知事件,才可以监听到UITextField的值改变事件
2.举例说明:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];
- (void)dealloc{
[super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 35)];
textField.borderStyle=UITextBorderStyleRoundedRect;
textField.contentHorizontalAlignment=UIControlContentVerticalAlignmentCenter;
//UITextFieldTextDidChangeNotification通知事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];
//注册UIControlEventValueChanged事件
[textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventValueChanged];
//添加到页面
[self.view addSubView: textField];
[textField release];
}
- (void)textFieldChange:(NSNotification*)notifice{
UITextField *field=[notifice object];
NSLog(@"text=%@",field.text);
}