textDidChange not called ( NSTextFieldDelegate )



步骤1。在xib 中添加NSTextField

步骤2。在.h文件中添加NSTextFieldDelegate,Control将NSTextField拖动到文件的所有者,以将委托设置为

步骤3,在.m文件中添加方法:

- (void)textDidChange:(NSNotification *)notification{
    NSLog(@"textDidChange");
}

但是方法textDidChange:没有调用?

有什么错误吗?

文件的所有者不是应用程序委托——应用程序委托是你放置该方法的地方吗?您应该控制拖动到标有应用程序委派的蓝色多维数据集。

编辑后:代理收到的消息是controlTextDidChange:而不是textDidChange,所以改为实现它。

您需要注册一个观察器来侦听NSNotification

// When the NSWindow is displayed, register the observer.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidChange:) name:NSControlTextDidChangeNotification object:nil];
- (void)controlTextDidChange:(NSNotification *)obj {
    // You can get the NSTextField, which is calling the method, through the userInfo dictionary.
    NSTextField *textField = [[obj userInfo] valueForKey:@"NSFieldEditor"];
}

看起来,NSFieldEditor返回的对象是NSTextView,而不是您可能期望的相同的NSTextField对象。

但是,根据Apples文档,如果您实现了此方法,并且控件委托已注册到此对象,则通知将自动注册。

控件发布NSControlTextDidChangeNotification通知,如果控件的委托实现了此方法,它将自动注册以接收通知

来源:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/controlTextDidChange:

相关内容

  • 没有找到相关文章

最新更新