我现在有一个小问题。我想在NSTextField中按下Enter键时执行一种方法。用户应该能够输入他的数据,并且一旦按下回车键就应该执行一种计算方法。
Answers:
您可以通过设置文本字段的操作来做到这一点。在IB中,将文本字段的选择器连接到您的控制器或呈现您要使用的IBAction的任何对象。
要在代码中进行设置,请向NSTextField发送一条setTarget:
消息和一条setAction:
消息。例如,如果要在代码中的控制器对象上设置此属性,并且textField出口称为myTextField:
- (void)someAction:(id)sender
{
// do something interesting when the user hits <enter> in the text field
}
// ...
[myTextField setTarget:self];
[myTextField setAction:@selector(someAction:)];
tab
不在现场,否则它也会执行操作。
您只需要这样做
对于某些键(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:
将由转义键触发。
在您的委托(NSTextFieldDelegate)中,添加以下内容:
-(void)controlTextDidEndEditing:(NSNotification *)notification
{
// See if it was due to a return
if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
{
NSLog(@"Return was pressed!");
}
}
@ M.ShuaibImran的解决方案的Swift 3版本:
首先将ViewController子类化为:NSTextFieldDelegate
class MainViewController: NSViewController, NSTextFieldDelegate {
...
}
在viewDidLoad()中将textField委托分配给ViewController:
self.textField.delegate = self
包括用于处理键盘响应程序的NSTextFieldDelegate方法:
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
}
let resultLength = String(result).count
如果要计数的项目已经存在,那么String
无需将其转换为String
,然后添加这行:yourNSTextField.currentEditor()?.selectedRange = NSRange(location: resultLength, length: 0)
然后是这一行:yourNSTextField.displayIfNeeded()
光标将跳到显示内容的最后一个字符的后面
NSTextFieldDelegate
的-control:textView:doCommandBySelector:
是你的朋友。
查找insertNewline:
命令。
setAction:
在上面使用过),但我发现很多有用的东西在哪里insertNewline:
。
在Interface Builder中-单击您的NSTextField,转到连接编辑器,从选择器拖动到控制器对象-您应该执行的操作!
最好的方法是将NSTextField
值绑定到NSString
属性。
例如,定义一个方法:
(void)setTextFieldString:(NSString *)addressString {}
添加绑定:
[textField bind:@"value" toObject:self withKeyPath:@"self.textFieldString" options:nil];
输入任何文本,然后按回车键,setTextFieldString
将被调用。