xcode 刷新视图,显示和 setNeedsDisplay 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4155249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:54:47  来源:igfitidea点击:

Refresh view with display and setNeedsDisplay not working

objective-cxcodemacos

提问by salomons

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:YESand 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!

我在模拟期间更新我的 customView 对象时遇到问题。模拟完成后弹出窗口。我希望它在模拟过程中自我更新。为此,我使用setNeedsDisplay:YES并且我也尝试过display。然而,这些都不适合我。有谁知道我应该如何让它工作?正如您在下面看到的,我尝试为更新以及使用 NSOperations 创建一个新线程。感谢帮助!

//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");
        }
    }

in my viewer class:

在我的查看器类中:

- (void) redraw {
    //[self setNeedsDisplay:YES];
    [self display];
}

回答by Chris Becke

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 cocoato be required reading when trying to implemented threaded cocoa apps.

要解决这个问题,您需要将模拟移动到工作线程,然后使用performSelectorOnMainThread:调用主线程上的重绘。我发现在尝试实现线程化可可应用程序时需要阅读这篇关于可可线程化的文章