在 Cocoa macOS 应用程序中,是否可以在选择更改期间而不是仅在更改结束时收到通知?
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?
我想连续跟踪 NSTextView 的选择,但我只在选择完成更改时才成功获得更改:
- (void)textViewDidChangeSelection:(NSNotification *)notification {
}
有没有办法持续跟踪选择的变化?任何帮助是极大的赞赏。谢谢
我通过子类化 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];
}
}
在我看来,这是一个很好的解决方案,效果很好。谢谢
我想连续跟踪 NSTextView 的选择,但我只在选择完成更改时才成功获得更改:
- (void)textViewDidChangeSelection:(NSNotification *)notification {
}
有没有办法持续跟踪选择的变化?任何帮助是极大的赞赏。谢谢
我通过子类化 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];
}
}
在我看来,这是一个很好的解决方案,效果很好。谢谢