NSTextField输入密钥检测或firstResponder检测 [英] NSTextField enter key detection or firstResponder detection

查看:263
本文介绍了NSTextField输入密钥检测或firstResponder检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 NSTextField s: textFieldUserID textFieldPassword



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

   - (void)controlTextDidEndEditing:(NSNotification *)aNotification 


$ b b

textFieldPassword 具有焦点并按下回车键时,将调用此委托。这正是我想要的。



我的问题是 controlTextDidEndEditing 也会在 textFieldPassword 有焦点,我将焦点移动到 textFieldUserID (通过鼠标或tab键)。这不是我想要的。



我尝试使用 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){
//根据first-responder状态做一些事情
NSLog(@is true);
}
}

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

解决方案

如果我正确理解了你,你可以为密码文本字段设置一个操作,并告诉字段只有当用户类型返回。首先,当用户在密码字段中键入 Return 时,在负责行为的类中声明并实现一个操作。例如:

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

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

使文本字段仅在返回中发送其操作,对于给定的 IBAction 和目标的操作可以在Interface Builder中进行,也可以通过编程实现。



,使用Attributes Inspector,选择 Action:Sent on Sent Only ,然后将文本字段操作链接到实现它的对象中的 IBAction



如果您希望以编程方式进行,请:



<$> p $ p> //使文本字段仅在按下Return时发送其操作
[passwordTextFieldCell setSendsActionOnEndEditing:NO];

//根据SomeClass中定义的操作的动作选择器
[passwordTextFieldCell setAction:@selector(returnOnPasswordField :)];

// someObject是实现动作的对象
[passwordTextFieldCell setTarget:someObject];


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!

解决方案

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];

这篇关于NSTextField输入密钥检测或firstResponder检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆