我有两个NSTextField
s: textFieldUserID
and textFieldPassword
.
For textFieldPassword
,我有一个代表如下:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
该代表在以下情况下被调用textFieldPassword
获得焦点后我按下了 Enter 键。这正是我想要的。
我的问题是controlTextDidEndEditing
也被调用时textFieldPassword
有焦点,我将焦点移至textFieldUserID
(通过鼠标或 Tab 键)。这不是我想要的。
我尝试使用controlTextDidChange
通知(每次按键都会调用一次),但我无法弄清楚如何检测回车键([textFieldPassword stringValue]
不包括回车键)。有人可以帮我解决这个问题吗?
我也尝试检测是否textFieldUserID
was a 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");
}
}
我确实需要一些帮助!
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.
在 Interface Builder 中,使用 Attributes Inspector,选择操作:仅在输入时发送,然后将文本字段操作链接到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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)