tar*_*eld 15 events callback nstextview
我对mac开发很新(来自网络和iOS背景)我无法弄清楚每次NSTextView的值发生变化时我都能得到通知.有任何想法吗?
lbr*_*dnr 38
我刚刚看到你想从NSTextView而不是NSTextField回调
只需添加对象的标题,该标题应该是委托协议
@interface delegateAppDelegate : NSObject <NSApplicationDelegate, NSTextViewDelegate> {
NSWindow *window;
}
Run Code Online (Sandbox Code Playgroud)
之后你添加一个像这样的方法
-(void)textDidChange:(NSNotification *)notification {
NSLog(@"Ok");
}
Run Code Online (Sandbox Code Playgroud)
确保已将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
Run Code Online (Sandbox Code Playgroud)