Why is [[NSScrollView documentView] scrollPoint:] not working in loadView method?

被刻印的时光 ゝ 提交于 2020-01-16 18:04:13

问题


I am trying to automatically scroll to the far right side of a horizontal imagebrowser in the loadView method of the view controller that contains the browser. It doesn't work when I try to do this (just scrolling to x=1000 as a test):

- (void)loadView {
    [super loadView];
    // Set initial position
    [[[self scrollView] documentView] scrollPoint:NSMakePoint(1000.0, 0.0)];
}

But at some point in the course of debugging I found that this works:

- (void)loadView {
    [super loadView];
    // Set initial position
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollToRight) userInfo:nil repeats:NO];
}

Where scrollToRight is just an instance method that does the same thing as above.

Does anybody know why this is the case? Is it because I'm doing it in the loadView method? Can scrollPoint: not be called from the main thread?

Update: I did some additional experimenting, like subscribing to an NSViewFrameDidChangeNotificaiton and scrolling based on that, but that doesn't work either. So far the only ways I've been able to get the scroll to happen is with NSTimer, or by calling the view controllers scrollToRight method from the parent window controller.


回答1:


Make sure [ [ self scrollView ] documentView ]nil--it's probably still nil. Also, you must call -scrollPoint from the main thread--all AppKit calls must be made from the main thread.



来源:https://stackoverflow.com/questions/13326617/why-is-nsscrollview-documentview-scrollpoint-not-working-in-loadview-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!