Events NSTextView值已更改

Events NSTextView值已更改,events,callback,nstextview,Events,Callback,Nstextview,我对mac开发相当陌生(来自web和iOS背景),我不知道如何在每次NSTextView的值更改时获得通知。有什么想法吗?设置nstextfield的委托。在委托的.h文件中添加委托协议 在.m文件中添加一个类似于-(void)controltextdichange:(NSNotification*)obj的方法{ NSLog(@“正常”); } 我希望这有助于设置代理,然后使用 - (void) controlTextDidChange: (NSNotification *) notifica

我对mac开发相当陌生(来自web和iOS背景),我不知道如何在每次NSTextView的值更改时获得通知。有什么想法吗?

设置nstextfield的委托。在委托的.h文件中添加委托协议 在.m文件中添加一个类似于
-(void)controltextdichange:(NSNotification*)obj的方法{
NSLog(@“正常”);
}


我希望这有助于设置代理,然后使用

- (void) controlTextDidChange: (NSNotification *) notification
{
}

Ups我刚才看到您需要的是来自NSTextView而不是NSTextField的回调

只需添加对象的头,该头应该是协议的委托

@interface delegateAppDelegate : NSObject <NSApplicationDelegate, NSTextViewDelegate> {
    NSWindow *window;
}
确保已将NSTextView(非NSScrollView)的委托属性与应接收委托的对象连接起来

以下是解决方案:

NSTextView *textView = ...;

@interface MyClass : NSObject<NSTextStorageDelegate>
@property NSTextView *textView;
@end

MyClass *myClass = [[MyClass alloc] init];
myClass.textView = textView;
textView.textStorage.delegate = myClass;

@implementation MyClass
- (void)textStorageDidProcessEditing:(NSNotification *)aNotification
{
   // self.textView.string will be the current value of the NSTextView
   // and this will get invoked whenever the textView's value changes,
   // BOTH from user changes (like typing) or programmatic changes,
   // like textView.string = @"Foo";
}
@end
NSTextView*textView=。。。;
@接口MyClass:NSObject
@属性NSTextView*textView;
@结束
MyClass*MyClass=[[MyClass alloc]init];
myClass.textView=textView;
textView.textStorage.delegate=myClass;
@MyClass的实现
-(无效)textStorageDidProcessEditing:(NSNotification*)通知
{
//self.textView.string将是NSTextView的当前值
//只要textView的值发生变化,就会调用它,
//无论是用户更改(如键入)还是程序更改,
//比如textView.string=@“Foo”;
}
@结束

就是这样!我很确定这是我尝试过的事情之一,但显然没有完全正确。现在可以工作了:-)谢谢!NSTextViewDelegate实现了NSTextDelegate这就是它的工作原理,但是您也可以使用特定的NSTextViewDelegate方法,如-(BOOL)textView:(NSTextView*)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString*)replacementStringThis将仅从直接与NSTextView交互的用户处获取更改(例如,用户在textview中键入或复制粘贴到其中或从中剪切)。如果您在
textView.string=@“Foo”中以编程方式更改textView,它将不会捕获对textView的更改。为此,您需要成为textview的textStorage的代理,如
textview.textStorage.delegate=self
并对自身对象的类实施
-(void)textStorageWillProcessEditing:(NSNotification*)加密。这将得到用户驱动的更改和直接属性设置器更改。谢谢Joel,查找此信息时遇到问题。这是针对NSTextField而不是NSTEXTVIEW的。NSTEXTVIEW是NSTextField的子类,其子类也是delegates@kdogisthebestNSTextView不是NSTextField的子类。这是针对NSTextField而不是NSTextView的。NSTextView是NSTextField的子类,它的委托人也是。恐怕很明显,NSTextView继承了NSTextView从NSText继承自NSView。NSTextField继承自NSControl,后者也继承自NSView。controlTextDidChange是NSControl的委托方法,这意味着NSTextField可以访问controlTextDidChange,但NSTextView不能,因为它不从NSControl或NSTextField继承。
NSTextView *textView = ...;

@interface MyClass : NSObject<NSTextStorageDelegate>
@property NSTextView *textView;
@end

MyClass *myClass = [[MyClass alloc] init];
myClass.textView = textView;
textView.textStorage.delegate = myClass;

@implementation MyClass
- (void)textStorageDidProcessEditing:(NSNotification *)aNotification
{
   // self.textView.string will be the current value of the NSTextView
   // and this will get invoked whenever the textView's value changes,
   // BOTH from user changes (like typing) or programmatic changes,
   // like textView.string = @"Foo";
}
@end