我有一个带for()循环的方法.在该循环中,mylabel.text会多次更新.但是,在方法完成之前,实际标签不会在屏幕上更新,并使用for()循环中创建的最后一个值进行更新.
使用NSLog,它在for()循环的中间进行更新,我看到标签的值确实多次改变.
iOS中的一般做法是不在for()循环中更新标签吗?我想有办法做到这一点.
这是问题所在:我有一些类似的代码
otherWinController = [[NotificationWindowController alloc] init];
for (int i = 0; i < 10; i++) {
[otherWinController showMessage:[NSString stringWithFormat:@"%d", i]];
NSLog(@"%d", i);
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
其他的WinController是NSWindowController的子类,我用它来更新我的窗口,因为代码中发生了更改,而alloc init方法只是打开了nib并显示了窗口.showMessage是一种更改NSTextView以显示参数中的任何文本的方法.
在NSLog中,文本每秒都会更改,只计为10.但是对于showMessage方法,文本为空整整十秒,然后只显示数字10.任何想法?
FTR,showMessage方法很简单
- (void)showMessage:(NSString *)text {
[[self message] setStringValue:text];
}
Run Code Online (Sandbox Code Playgroud)
并不重要,这是非常基本的.
只是希望屏幕根据奇数或偶数条件反复闪烁蓝色/红色一段时间,但它只运行一次,而不是30,000次.我错过了什么?
-(IBAction) changeBackgroundColor:(id)sender
{
for (int y = 0; y < 30000; y++)
{
if(y % 2)
{
self.view.backgroundColor = [UIColor blueColor];
colorView.backgroundColor = [UIColor redColor];
} else {
self.view.backgroundColor = [UIColor redColor];
colorView.backgroundColor = [UIColor blueColor];
}
}
}
Run Code Online (Sandbox Code Playgroud) 我意识到这是一个非常基本的问题,其他帖子似乎没有帮助,因为我对此很新.我很好奇为什么A.)我的for循环不会遍历我的字符串(问题),为什么B.)它只打印最后一个问题"你的姓氏是什么?" 当我点击我的目标操作按钮时,在我的iOS模拟器上.我的所有关系都是正确的,显然如果它的建筑物,我的.h中的所有东西都可以.
我很感激帮助!
- (IBAction) question;
{
for (int i = 0; i < 5; i++)
{
questionLabel.text = @"Whats your name?";
questionLabel.text = @"Whats your age?";
questionLabel.text = @"Whats your height?";
questionLabel.text = @"Whats your weight?";
questionLabel.text = @"Whats your last name?";
}
}
Run Code Online (Sandbox Code Playgroud)