Objective c 为什么[[NSScrollView documentView]scrollPoint:]在loadView方法中不起作用?
我试图在包含浏览器的视图控制器的loadView方法中自动滚动到水平图像浏览器的最右侧。当我尝试这样做时,它不起作用(只是滚动到x=1000作为测试): 但在调试过程中的某个时刻,我发现这是可行的:Objective c 为什么[[NSScrollView documentView]scrollPoint:]在loadView方法中不起作用?,objective-c,macos,cocoa,Objective C,Macos,Cocoa,我试图在包含浏览器的视图控制器的loadView方法中自动滚动到水平图像浏览器的最右侧。当我尝试这样做时,它不起作用(只是滚动到x=1000作为测试): 但在调试过程中的某个时刻,我发现这是可行的: - (void)loadView { [super loadView]; // Set initial position [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sc
- (void)loadView {
[super loadView];
// Set initial position
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollToRight) userInfo:nil repeats:NO];
}
其中scrollToRight只是一个执行与上面相同操作的实例方法
有人知道为什么会这样吗?是不是因为我在loadView方法中执行此操作?不能从主线程调用scrollPoint:吗
更新:我做了一些额外的实验,比如订阅NSViewFramedChangeNotification并在此基础上滚动,但这也不起作用。到目前为止,我能够实现滚动的唯一方法是使用NSTimer,或者从父窗口控制器调用view controller scrollToRight方法。确保
[[self scrollView]documentView]
≠ <代码>零——可能还是零。另外,您必须从主线程调用-scrollPoint
——所有AppKit调用都必须从主线程进行。Hmm,documentView在我试图设置scrollPoint的位置不是nil。结果是。此外,我还询问了线程,因为它作为NSTimer中的选择器方法完成时确实可以工作。这是在主线程上吗?@bellkev:取决于您计划计时器的线程。(另外,“选择器方法”没有意义。选择器是方法的名称。)
- (void)loadView {
[super loadView];
// Set initial position
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollToRight) userInfo:nil repeats:NO];
}