NSTextField等待,直到循环结束更新 [英] NSTextField waits until the end of a loop to update

查看:192
本文介绍了NSTextField等待,直到循环结束更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的问题:我有一些像这样的代码

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方法只是打开了nib并显示窗口。 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.

推荐答案

如果你明确给运行循环运行一些时间,你可以在循环中实现想要的效果:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆