简体   繁体   中英

In a Cocoa Application for macOS, is it possible to get notified during a selection change and not only at the end of the change?

I would like to track the selection of a NSTextView continuously, but I only succeed to get the change when the selection finishes changing using:

- (void)textViewDidChangeSelection:(NSNotification *)notification {

}

Is there a way to track selection changes continuously? Any help is greatly appreciated. Thanks

I succeeded to solve the issue by subclassing NSTextView and overriding the following method:

-(void)setSelectedRanges:(NSArray<NSValue *> *)selectedRanges affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelecting {

    [super setSelectedRanges:selectedRanges affinity:affinity stillSelecting:stillSelecting];

    if (stillSelecting && [self delegate] && [[self delegate] respondsToSelector:@selector(textViewDidChangeSelection:)]) {
        NSNotification *note = [[NSNotification alloc] initWithName:@"TextViewSelectionIsChangingNotification" object:self userInfo:nil];
        [[self delegate] textViewDidChangeSelection:note];
    }

}

This seems to me a good solution, it works well. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM