简体   繁体   English

NSTextField辞职第一响应者

[英]NSTextField resigning first responder

I'm trying to have my text field resign first responder when the user is done editing it, like when they hit enter. 当用户完成编辑时,我正试图让我的文本字段重新响应第一响应者,就像他们点击进入时一样。 The delegate method is getting called but what I have now doesn't work, what is the proper way to go about doing this? 委托方法被调用,但我现在所做的不起作用,这样做的正确方法是什么?

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
    [fieldEditor resignFirstResponder];
    return YES;
}

From the docs on the -resignFirstResponder method: 从-resignFirstResponder方法的文档:

Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. 使用NSWindow makeFirstResponder:方法而不是此方法将对象作为第一个响应者。 Never invoke this method directly. 永远不要直接调用此方法。

This code should do it: 这段代码应该这样做:

[myWindow makeFirstResponder:nil];
- (void)controlTextDidEndEditing:(NSNotification *)obj {

   [self.window makeFirstResponder:view];
}

where "view" is the parent NSView of your NSTextView control. 其中“view”是NSTextView控件的父NSView。

I believe you are looking for the NSControl delegate method: 我相信你正在寻找NSControl委托方法:

- (void)controlTextDidEndEditing:(NSNotification *)notif

Implement this method in your NSTextField delegate (easily set in IB). NSTextField委托中实现此方法(在IB中轻松设置)。

Then your request to change the responder, such as 然后你的请求改变响应者,如

[self.window selectNextKeyView:self]

should work. 应该管用。

I am no expert in iPhone programming yet but the method you need to implement is "textFieldShouldReturn" implemented like this: 我不是iPhone编程专家,但你需要实现的方法是“textFieldShouldReturn”实现如下:

 - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM