2010-04-22 2 views
4

서브 클래스가 NSScrollView이고 현재 스크롤 위치를 기반으로 다른 NSView을 업데이트하고 싶습니다. 나는 value의 을 관찰하면서 KVC를 시도했지만 그 전화는 결코 부르지 않는다. NSScroller 변경 사항을 준수하는 방법은 무엇입니까?

// 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 
    } 
} 

은 당신이 NSScrollview의 스크롤을 관찰하는 방법의 더 나은 방법을 내 추론의 오류를 참조하거나 알고 계십니까?

답변

6

경계 변경 알림을 게시하고 NSViewBoundsDidChangeNotification을 수신 대기하도록 스크롤보기의 콘텐츠보기에 알립니다.

NSPoint currentScrollPosition = [[theScrollView contentView] bounds].origin; 
다음 Scroll View Programming Guide에 명시된 바와 같이

[[aScrollView contentView] setPostsBoundsChangedNotifications:YES]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(boundsDidChangeNotification:) 
              name:NSViewBoundsDidChangeNotification 
              object:[scrollView contentView]]; 

것은, 현재 스크롤 위치이 방법을 얻을 수 있습니다

관련 문제