NSTextField 等到循环结束才更新 [英] NSTextField waits until the end of a loop to update
问题描述
问题来了:我有一些代码是这样的
Here's the problem: I have some code that goes like this
otherWinController = [[NotificationWindowController alloc] init];
for (int i = 0; i < 10; i++) {
[otherWinController showMessage:[NSString stringWithFormat:@"%d", i]];
NSLog(@"%d", i);
sleep(1);
}
其中 otherWinController 是 NSWindowController 的子类,我用来在代码发生更改时更新我的窗口,alloc init 方法只是打开笔尖并显示窗口.showMessage 是一种更改 NSTextView 以显示参数中的任何文本的方法.
where otherWinController is a subclass of NSWindowController that I am using to update my window as changes happen in code, and the alloc init method simply opens up the nib and shows the window. showMessage is a method that changes an NSTextView to display whatever text is in the parameter.
在 NSLog 中,文本每秒都在变化,并且只计数到十.但是对于 showMessage 方法,文本是空白的整整十秒钟,然后只显示数字 10.有什么想法吗??
in the NSLog, the text changes every second and just counts to ten. However for the showMessage method, the text is blank for a full ten seconds and then just displays the number 10. any thoughts??
FTR,showMessage 方法很简单
FTR, the showMessage method is simply
- (void)showMessage:(NSString *)text {
[[self message] setStringValue:text];
}
这并不重要,这很基本.
not that it should matter, that's pretty basic.
推荐答案
如果你明确地给 run loop 一些运行时间,你可能就可以在你的循环中达到预期的效果:
You can probably achieve the desired effect right inside your loop, if you explicitly give the run loop some time to run:
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 0.1]];
这篇关于NSTextField 等到循环结束才更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!