【发布时间】:2013-12-20 19:37:01
【问题描述】:
我刚开始使用 Cocoa 进行 OS X 开发,但遇到了很多基本问题。
如何检测从 NSTextField 更改的文本,例如 Java 中的 onTextChanged()?
有人说make delegate,但我不明白什么是delegate,它是做什么的。
我在 Xcode 5.0.2 中使用可可框架。
谢谢,对不起我的英语不好:'(
【问题讨论】:
标签: macos cocoa nstextfield
我刚开始使用 Cocoa 进行 OS X 开发,但遇到了很多基本问题。
如何检测从 NSTextField 更改的文本,例如 Java 中的 onTextChanged()?
有人说make delegate,但我不明白什么是delegate,它是做什么的。
我在 Xcode 5.0.2 中使用可可框架。
谢谢,对不起我的英语不好:'(
【问题讨论】:
标签: macos cocoa nstextfield
在类声明中添加 NSTextFieldDelegate 协议:
@interface MyView : NSView <NSTextFieldDelegate>
然后在代码集中:
myTextField.delegate = self;
现在您的文本字段将向委托发送通知,您可以实现任何您想要的委托功能(请参阅下面链接中的 NSText 委托方法实现)
你说你现在需要的是实现:
- (void)textDidChange:(NSNotification *)aNotification {
//Do stuff here
}
【讨论】:
Cannot find interface declaration for 'UIView', superclass of 'MyView'; did you mean 'NSView'?
NSTextFieldDelegate 没有 textDidChange: 方法。相反,应该使用controlTextDidChange:,一种NSControl 委托方法。
尽管编写代码,您也可以通过绑定来实现相同的功能。只需将您的textfield 连接到fileowners 的代表即可。如下所示:-
然后实现下面这个方法。这样就不需要在头文件中定义NSTextFieldDelegateprotocol,也不需要在实现文件中通过代码设置delegate:-
-(void)controlTextDidChange:(NSNotification *)obj
{
}
【讨论】: