【问题标题】: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?在 macOS 的 Cocoa 应用程序中,是否可以在选择更改期间而不是在更改结束时收到通知?
【发布时间】:2020-08-19 20:12:44
【问题描述】:

我想连续跟踪 NSTextView 的选择,但我只有在选择完成更改后才能成功获取更改:

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

}

有没有办法连续跟踪选择变化?任何帮助是极大的赞赏。谢谢

【问题讨论】:

    标签: cocoa nstextview


    【解决方案1】:

    我通过继承 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];
        }
    
    }
    

    在我看来,这是一个很好的解决方案,效果很好。谢谢。

    【讨论】:

      猜你喜欢
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多