我有一个带有一些 NSTextFields 的 Mac 应用程序(SDK 10.10):
由于我需要在文本字段获得和退出焦点时得到通知,因此我将 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
说
通知接收者它被要求在其窗口中放弃其作为第一响应者的状态。
那么为什么会resignFirstResponder
召唤新的第一响应者而不是旧的第一响应者呢?