12

When I create UITextField inside Interface Builder, I can access Events tab for it, which has events like Value changed, Touch cancel, Touch drag, etc. I can assign my own methods to every of those events. How can I do the same, when I create UITextField programmatically with alloc?

Mike
  • 1,712
  • 3
  • 17
  • 17
  • Hi, I've created a UITextField in Interface Builder and want to register UIControlEventValueChanged. How do you go about it? How do you define that function? – Namratha Feb 11 '11 at 07:04

3 Answers3

14

Refer to Apple documentation for UIControl. After initializing your textField, call addTarget:action:forControlEvents:

example for the touch event ending an edit session

[textField addTarget:self action:@selector(handleTouchValueChanged:) forControlEvents: UIControlEventEditingDidEnd]
falconcreek
  • 4,170
  • 1
  • 21
  • 22
11

Instead of UIControlEventValueChanged, you should use UIControlEventEditingChanged:

[_titleTextField addTarget:self action:@selector(handleTitleValueChanged:) forControlEvents:UIControlEventEditingChanged];
Cristik
  • 30,989
  • 25
  • 91
  • 127
Baryon Lee
  • 1,157
  • 11
  • 11
  • 1
    from hajikelist's answer (belongs here as a comment instead of being an answer itself): UIControlEventEditingChanged fires whenever user changes value [synchronous with typing or keyup] which could cause extra hits to your data handler routine, if you're saving values based on that event, expecting it to be some kind of final value from user input...(you could end up with unexpected behavior, like having multiple saves for each textfield.) – GeneralMike Oct 31 '12 at 20:07
3

UIControlEventEditingChanged fires whenever user changes value [synchronous with typing or keyup] which could cause extra hits to your data handler routine, if you're saving values based on that event, expecting it to be some kind of final value from user input...

axel22
  • 32,045
  • 9
  • 125
  • 137
hajikelist
  • 1,136
  • 9
  • 9
  • 1
    A good point, but should probably be a comment under one of the other answers instead of an answer itself, since you didn't specify an alternative to those answers. – GeneralMike Oct 31 '12 at 20:04