开发者

NSTextView value changed

开发者 https://www.devze.com 2023-02-15 12:03 出处:网络
I\'m pretty new to mac development (coming from a web and iOS background) and I can\'t work out how I could get a notification every time the value of an NSTextView chan开发者_开发百科ges. Any ideas?U

I'm pretty new to mac development (coming from a web and iOS background) and I can't work out how I could get a notification every time the value of an NSTextView chan开发者_开发百科ges. Any ideas?


Ups I just saw that you want a callback from NSTextView and not NSTextField

Just add in the header of the object which should be the delegate the protocol

@interface delegateAppDelegate : NSObject <NSApplicationDelegate, NSTextViewDelegate> {
    NSWindow *window;
}

After that you add a method like

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

Make sure you connected the delegate property of the NSTextView (not NSScrollView) with the object which should receive the delegate


Here's the solution:

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


set the nstextfield's delegate. in the .h file of the delegate you add the delegate protocol In the .m file you add a method like -(void)controlTextDidChange:(NSNotification *)obj { NSLog(@"ok"); }

I hope that helps


Set the delegate and then use

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

精彩评论

暂无评论...
验证码 换一张
取 消