I have an NSScrollView
subclass and I would like to update another NSView
based on the current scroll position. I tried KVC-observing the value
of [self horizontalScroller]
but that never gets called.
// In awakeFromNib
[[self horizontalScroller] addObserver:self
forKeyPath:@"value"
options:NSKeyValueObservingOptionNew
context:NULL];
// Later in the file
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (object == [self horizontalScroller] && [keyPath isEqualToString:@"value"]) {
// This never gets called
}
}
Do you see an error in my reasoning or know a better method of how to observe the scrolling of an NSScrollview
?
Tell the scroll view's content view to post bounds changed notifications, then listen for NSViewBoundsDidChangeNotification.
[[aScrollView contentView] setPostsBoundsChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(boundsDidChangeNotification:)
name:NSViewBoundsDidChangeNotification
object:[scrollView contentView]];
As stated in the Scroll View Programming Guide, you can get the current scroll position this way:
NSPoint currentScrollPosition = [[theScrollView contentView] bounds].origin;