使用display和setNeedsDisplay刷新视图不起作用 [英] Refresh view with display and setNeedsDisplay not working
问题描述
我在仿真过程中无法更新我的customView对象.模拟完成后,将弹出窗口.我希望它在仿真过程中进行自我更新.为此,我使用了setNeedsDisplay:YES
,我也尝试了display
.但是,这些都不对我有用.有谁知道我应该如何工作?如您在下面看到的,我尝试创建一个用于更新以及使用NSOperations的新线程.感谢帮助!
I am having problems updating my customView object during simulation. The window pops up after the simulation is done. I would like it to update itself during the simulation. For this I use setNeedsDisplay:YES
and I have also tried display
. None of this works for me however. Does anyone have an idea how I should get this working? As you can see below I have tried to create a new thread for the updating as well as using NSOperations. Grateful for help!
//Run simulation
for (int iteration=0; iteration<numberOfIterations; iteration++){
//NSInvocationOperation *update = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updatePopulation) object:nil];
//NSInvocationOperation *draw = [[NSInvocationOperation alloc] initWithTarget:view selector:@selector(redraw) object:nil];
//[draw addDependency:update];
//[queue addOperation:update];
//[queue addOperation:draw];
[NSThread sleepForTimeInterval:0.01]; //to make it easer to see..
[self updatePopulation];
//[view redraw];
[NSThread detachNewThreadSelector:@selector(redraw) toTarget:view withObject:nil];
//[self performSelector:@selector(updatePopulation) withObject:nil afterDelay:1];
//[view performSelector:@selector(redraw) withObject:nil afterDelay:1];
//Save segregation
if (iteration%(numberOfIterations/100) == 0) {
printf("hej\n");
}
}
在我的查看器类中:
- (void) redraw {
//[self setNeedsDisplay:YES];
[self display];
}
推荐答案
您似乎正在尝试在辅助线程上绘画.
It looks like you are trying to do the painting on the worker thread.
据我所知,这不受支持.
This is not, as far as I am aware, supported.
要解决此问题,您需要将模拟移至工作线程,然后使用这篇关于可可线程化的文章,在尝试实现线程可可应用时必须阅读.
To solve this, you need to move your simulation to a worker thread, and then use performSelectorOnMainThread: to invoke the redraw on the main thread. I find this article on threading in cocoa to be required reading when trying to implemented threaded cocoa apps.
这篇关于使用display和setNeedsDisplay刷新视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!