我应该使用哪种委托方法来响应文本字段的点击?

时间:2022-02-12 20:03:40

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

我想在用户点击文本字段时打开一个面板。我想我应该使用一个响应click事件的委托方法。我发现了

- (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?

对不起,我想我想在mac上使用它,而不是在iphone上,如何使用cocoa?

2 个解决方案

#1


5  

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

textFieldDidBeginEditing:delegate方法仅在用户开始编辑UITextField内的文本时触发,如方法名称所示。

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

如果要在触摸UITextField时触发方法,则应尝试以下操作:

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

- (void) textFieldTouched:(id)sender {
    // Display the panel
}

#2


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.

此方法通知委托指定的文本字段刚刚成为第一个响应者。

#1


5  

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

textFieldDidBeginEditing:delegate方法仅在用户开始编辑UITextField内的文本时触发,如方法名称所示。

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

如果要在触摸UITextField时触发方法,则应尝试以下操作:

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

- (void) textFieldTouched:(id)sender {
    // Display the panel
}

#2


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.

此方法通知委托指定的文本字段刚刚成为第一个响应者。