NSTextField输入键检测或firstResponder检测
我有两个 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 NSTextField
s: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您可以为密码文本字段设置一个操作,并告诉该字段仅在用户键入 Return 时发送其操作。首先,在负责用户在密码字段上键入 Return 时的行为的类中声明并实现一个操作。例如:
使文本字段仅在 Return 上发送其操作,并将该操作链接到给定的
IBAction
和目标,可以在 Interface Builder 中或以编程方式完成。在 Interface Builder 中,使用属性检查器,选择操作:仅在输入时发送,然后将文本字段操作链接到实现它的对象中的
IBAction
,可能是文件的所有者或第一响应者。如果您更愿意以编程方式执行此操作,那么:
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:
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: