NSTextField输入键检测或firstResponder检测

发布于 2024-11-14 08:47:43 字数 1072 浏览 1 评论 0原文

我有两个 NSTextField:textFieldUserID 和 textFieldPassword。

对于 textFieldPassword,我有一个委托,如下所示:

- (void)controlTextDidEndEditing:(NSNotification *)aNotification

textFieldPassword 获得焦点并且我按下 Enter 键时,将调用该委托。这正是我想要的。

我的问题是,当 textFieldPassword 具有焦点并且我将焦点移动到 textFieldUserID (通过鼠标或 Tab 键)时,也会调用 controlTextDidEndEditing 。这不是我想要的。

我尝试使用 controlTextDidChange 通知(每次按键都会调用一次),但我无法弄清楚如何检测回车键( [textFieldPassword stringValue] 不包括输入键)。有人可以帮我解决这个问题吗?

我还尝试检测 textFieldUserID 是否是 firstResponder,但它对我不起作用。这是我尝试过的代码:

if ( [[[self window] firstResponder] isKindOfClass:[NSTextView class]] &&
    [[self window] fieldEditor:NO forObject:nil] != nil ) {
    NSTextField *field = [[[self window] firstResponder] delegate];
    if (field == textFieldUserID) {
        // do something based upon first-responder status
        NSLog(@"is true");
    }
}

我当然可以在这里使用一些帮助!

I have two NSTextFields: textFieldUserID and textFieldPassword.

For textFieldPassword, I have a delegate as follows:

- (void)controlTextDidEndEditing:(NSNotification *)aNotification

This delegate gets called when textFieldPassword has focus and I hit the enter key. This is exactly what I want.

My problem is that controlTextDidEndEditing also gets called when textFieldPassword has focus and I move the focus to textFieldUserID (via mouse or tab key). This is NOT what I want.

I tried using controlTextDidChange notification (which is getting called once per key press) but I was unable to figure out how to detect enter key ( [textFieldPassword stringValue] does not include the enter key). Can someone please help me figure this one out?

I also tried to detect if textFieldUserID was a firstResponder, but it did not work for me. Here is the code I tried out:

if ( [[[self window] firstResponder] isKindOfClass:[NSTextView class]] &&
    [[self window] fieldEditor:NO forObject:nil] != nil ) {
    NSTextField *field = [[[self window] firstResponder] delegate];
    if (field == textFieldUserID) {
        // do something based upon first-responder status
        NSLog(@"is true");
    }
}

I sure could use some help here!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风苍溪 2024-11-21 08:47:43

如果我理解正确,您可以为密码文本字段设置一个操作,并告诉该字段仅在用户键入 Return 时发送其操作。首先,在负责用户在密码字段上键入 Return 时的行为的类中声明并实现一个操作。例如:

@interface SomeClass …
- (IBAction)returnOnPasswordField:(id)sender;
@end

@implementation SomeClass
- (IBAction)returnOnPasswordField:(id)sender {
    // do something
}
@end

使文本字段仅在 Return 上发送其操作,并将该操作链接到给定的 IBAction 和目标,可以在 Interface Builder 中或以编程方式完成。

在 Interface Builder 中,使用属性检查器,选择操作:仅在输入时发送,然后将文本字段操作链接到实现它的对象中的 IBAction,可能是文件的所有者或第一响应者。

如果您更愿意以编程方式执行此操作,那么:

// Make the text field send its action only when Return is pressed
[passwordTextFieldCell setSendsActionOnEndEditing:NO];

// The action selector according to the action defined in SomeClass
[passwordTextFieldCell setAction:@selector(returnOnPasswordField:)];

// someObject is the object that implements the action
[passwordTextFieldCell setTarget:someObject];

If I understood you correctly, you could set an action for the password text field and tell the field to send its action only when the user types Return. Firstly, declare and implement an action in the class responsible for the behaviour when the user types Return on the password field. For example:

@interface SomeClass …
- (IBAction)returnOnPasswordField:(id)sender;
@end

@implementation SomeClass
- (IBAction)returnOnPasswordField:(id)sender {
    // do something
}
@end

Making the text field send its action on Return only, and linking the action to a given IBAction and target, can be done either in Interface Builder or programatically.

In Interface Builder, use the Attributes Inspector, choose Action: Sent on Enter Only, and then link the text field action to an IBAction in the object that implements it, potentially the File’s Owner or the First Responder.

If you’d rather do it programatically, then:

// Make the text field send its action only when Return is pressed
[passwordTextFieldCell setSendsActionOnEndEditing:NO];

// The action selector according to the action defined in SomeClass
[passwordTextFieldCell setAction:@selector(returnOnPasswordField:)];

// someObject is the object that implements the action
[passwordTextFieldCell setTarget:someObject];
﹏半生如梦愿梦如真 2024-11-21 08:47:43
[passwordTextFieldCell setTarget:self];

[passwordTextFieldCell setAction:@selector(someAction:)];

- (void) someAction{

//handle

}
[passwordTextFieldCell setTarget:self];

[passwordTextFieldCell setAction:@selector(someAction:)];

- (void) someAction{

//handle

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文