我现在有一个小问题。我想在NSTextField中按下回车键时执行一个方法。用户应该能够输入他的数据,并在按下回车键后立即执行一个计算方法。
您可以通过设置文本字段的操作来实现此目的。在IB中,将文本字段的选择器连接到您的控制器或呈现所需使用的IBAction的任何对象。
要在代码中设置它,请向NSTextField发送setTarget:
消息和setAction:
消息。例如,如果您正在代码中为控制器对象设置此选项,并且您的textField outlet称为myTextField:
- (void)someAction:(id)sender
{
// do something interesting when the user hits <enter> in the text field
}
// ...
[myTextField setTarget:self];
[myTextField setAction:@selector(someAction:)];
你只需要做这件事
对于一些键(如Enter、Delete、Backspace等)
self.textfield.delegate = self;
然后实现这个方法
- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
if (commandSelector == @selector(insertNewline:)) {
//Do something against ENTER key
} else if (commandSelector == @selector(deleteForward:)) {
//Do something against DELETE key
} else if (commandSelector == @selector(deleteBackward:)) {
//Do something against BACKSPACE key
} else if (commandSelector == @selector(insertTab:)) {
//Do something against TAB key
} else if (commandSelector == @selector(cancelOperation:)) {
//Do something against Escape key
}
// return YES if the action was handled; otherwise NO
}
cancelOperation:
,它将由 Esc 键触发。 - Abhi BeckertSwift 3 / 4 / 5版本@M.ShuaibImran的解决方案:
首先将您的ViewController子类化为:NSTextFieldDelegate
class MainViewController: NSViewController, NSTextFieldDelegate {
...
}
在你的viewDidLoad()方法中将textField的代理分配给ViewController:
self.textField.delegate = self
包含处理键盘响应的NSTextFieldDelegate方法:
Include the NSTextFieldDelegate method that handles keyboard responders:func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if (commandSelector == #selector(NSResponder.insertNewline(_:))) {
// Do something against ENTER key
print("enter")
return true
} else if (commandSelector == #selector(NSResponder.deleteForward(_:))) {
// Do something against DELETE key
return true
} else if (commandSelector == #selector(NSResponder.deleteBackward(_:))) {
// Do something against BACKSPACE key
return true
} else if (commandSelector == #selector(NSResponder.insertTab(_:))) {
// Do something against TAB key
return true
} else if (commandSelector == #selector(NSResponder.cancelOperation(_:))) {
// Do something against ESCAPE key
return true
}
// return true if the action was handled; otherwise false
return false
}
在你的委托(NSTextFieldDelegate)中添加以下内容:
-(void)controlTextDidEndEditing:(NSNotification *)notification
{
// See if it was due to a return
if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
{
NSLog(@"Return was pressed!");
}
}
let resultLength = String(result).count
(如果要计数的项目已经是String
,当然不需要转换为String
),然后添加此行:yourNSTextField.currentEditor()?.selectedRange = NSRange(location: resultLength, length: 0)
,然后再添加这一行:yourNSTextField.displayIfNeeded()
光标将跳到您呈现内容的最后一个字符后面。 - AegerNSTextFieldDelegate
的control:textView:doCommandBySelector:
是你的好朋友。
寻找insertNewline:
命令。
setAction:
),但我发现了很多有用的地方,其中包括insertNewline:
。 - StuFF mc最佳实践是将 NSTextField
的值与 NSString
属性绑定。
例如,定义一个方法:
(void)setTextFieldString:(NSString *)addressString {}
[textField bind:@"value" toObject:self withKeyPath:@"self.textFieldString" options:nil];
输入任何文本并按回车键,将调用setTextFieldString
。