UITextField控件之ValueChanged事件

来源:互联网 发布:阿里云计算认证考试 编辑:程序博客网 时间:2024/06/05 01:06

From: http://wuchaorang.2008.blog.163.com/blog/static/48891852201402184156861/


1.UITextField的UIControlEventValueChanged事件单独注册,你会发现根本不执行,需要多注册一个通知事件,才可以监听到UITextField的值改变事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];

2.举例说明:

- (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);

}


0 0