问题
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?
回答1:
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
}
回答2:
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.
来源:https://stackoverflow.com/questions/680863/which-delegate-method-i-should-use-to-respond-to-clicks-on-a-text-field