简体   繁体   English

在 macOS 的 Cocoa 应用程序中,是否可以在选择更改期间收到通知,而不仅仅是在更改结束时收到通知?

[英]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:我想连续跟踪 NSTextView 的选择,但我只有在选择完成更改时才成功获得更改:

- (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:我通过子类化 NSTextView 并重写以下方法成功地解决了这个问题:

-(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.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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