gpt4 book ai didi

macos - NSResponder resignFirstResponder 在错误的对象上触发

转载 作者:行者123 更新时间:2023-12-03 16:35:36 24 4
gpt4 key购买 nike

我有一个带有一些 NSTextField 的 Mac 应用程序(SDK 10.10):

Xib file

由于我需要在文本字段获得和放弃焦点时收到通知,因此我对 NSTextField 进行了子类化:

@interface MyTextField : NSTextField
@end

@implementation MyTextField

- (BOOL)becomeFirstResponder
{
BOOL didBecomeFirstResponder = [super becomeFirstResponder];
NSLog(@"%@ didBecomeFirstResponder = %@", [self accessibilityLabel], didBecomeFirstResponder?@"YES":@"NO");
return didBecomeFirstResponder;
}

- (BOOL)resignFirstResponder
{
BOOL didResignFirstResponder = [super resignFirstResponder];
NSLog(@"%@ didResignFirstResponder = %@", [self accessibilityLabel], didResignFirstResponder?@"YES":@"NO");
return didResignFirstResponder;
}

@end

当运行此代码并在 3 个文本字段之间切换时,我在控制台中得到以下输出:

firstField didResignFirstResponder = YES
firstField didBecomeFirstResponder = YES
secondField didResignFirstResponder = YES
secondField didBecomeFirstResponder = YES
thirdField didResignFirstResponder = YES
thirdField didBecomeFirstResponder = YES
firstField didResignFirstResponder = YES
firstField didBecomeFirstResponder = YES
secondField didResignFirstResponder = YES
secondField didBecomeFirstResponder = YES

每次我按 TAB 键(或单击非事件文本字段之一)时,应用程序都会输出

<new first responder> didResignFirstResponder = YES
<new first responder> didBecomeFirstResponder = YES

难道不是

<old first responder> didResignFirstResponder = YES
<new first responder> didBecomeFirstResponder = YES

???

我这里有什么严重错误吗?

- (BOOL)resignFirstResponder 的文档说

Notifies the receiver that it’s been asked to relinquish its status as first responder in its window.

那么为什么要在新的第一响应者而不是旧的第一响应者上调用 resignFirstResponder 呢?

最佳答案

我遇到了同样的问题,但找不到解决方案,因此我最终解决了这个问题并使用 textDidEndEditing() 方法来达到相同的目的。快速代码:

MyTextFieldNSTextField 的子类,并向其中添加 myDelegate 属性,该属性应在文本字段失去焦点后处理逻辑。

class MyTextField : NSTextField
{

/// Custom delegate with other methods than NSTextFieldDelegate.
var myDelegate: MyTextFieldDelegate? = nil;

override func becomeFirstResponder() -> Bool {
let became = super.becomeFirstResponder();
if (became) {
self.myDelegate?.myTextFieldDidBecomeFirstResponder(self);
}
return became;
}

override func resignFirstResponder() -> Bool {
let resigned = super.resignFirstResponder();
if (resigned) {
self.myDelegate?.myTextFieldDidResignFirstResponder(self);
}
return resigned;
}

override func textDidEndEditing(obj: NSNotification) {
super.textDidEndEditing(obj);
self.myDelegate?.myTextFieldDidResignFirstResponder(self);
}

}

协议(protocol)本身:

protocol MyTextFieldDelegate
{

func myTextFieldDidBecomeFirstResponder(textField: MyTextField);

func myTextFieldDidResignFirstResponder(textField: MyTextField);
}

显然,您可以直接实现失去/获得焦点逻辑(例如绘制不同的边框等),而不是使用和调用委托(delegate)。

线索是 textDidEndEditing() 方法,当我单击另一个字段、按 Enter 或按 Tab 键退出时,它似乎工作得很好。

关于macos - NSResponder resignFirstResponder 在错误的对象上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27604111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com