Which delegate method I should use to respond to clicks on a text field?

耗尽温柔 提交于 2019-12-23 03:35:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!