1

I want to open a panel when the user clicks on a text field. I think I should use a delegate method that responds to the click event. I found that the

- (void)textDidBeginEditing:(NSNotification *)aNotification

method does not work, and that the

- (void)controlTextDidBeginEditing:(NSNotification *)aNotification

method works, but only when I edit the text in the text field, not then I click it. If I edit the text again, this method does not work. Why?


Sorry, I think I want to use this on mac , not on iphone,How to do with it with cocoa?

Emmett
  • 14,035
  • 12
  • 56
  • 81
jin
  • 2,145
  • 5
  • 27
  • 44

2 Answers2

5

The textFieldDidBeginEditing: delegate method only gets triggered when the user starts editing the text inside the UITextField, as the method name implies.

If you want to trigger a method when the UITextField is touched, you should try this:

[textField addTarget:self 
              action:@selector(textFieldTouched:)
    forControlEvents:UIControlEventTouchDown];

- (void) textFieldTouched:(id)sender {
    // Display the panel
}
Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
  • Sorry, I think I want to use this on mac , not on iphone,How to do with it with cocoa? – jin Mar 26 '09 at 01:15
  • @jin http://stackoverflow.com/questions/684166/which-delegate-method-should-i-use-to-respond-to-clicks-on-an-nstextfield/4473352#4473352 has pretty good reply, have a look. – Jais May 16 '13 at 07:06
3

The correct delegate method name is

- (void)textFieldDidBeginEditing:(UITextField *)textField

From the documentation:

This method notifies the delegate that the specified text field just became the first responder.

Alex
  • 26,829
  • 3
  • 55
  • 74