NSTextField waits until the end of a loop to update

前端 未结 4 1441
礼貌的吻别
礼貌的吻别 2020-11-27 20:52

Here\'s the problem: I have some code that goes like this

otherWinController = [[NotificationWindowController alloc] init];
for (int i = 0; i < 10; i++)          


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 21:18

    I think that by calling sleep(1) you block the main thread, which must draw your changes. So the display is not updated. The task manager will not interrupt your function. You shouldn't use sleep in this case. Please take a look at NSTimer class. It has a static method scheduledTimerWithTimeInterval, which you should use.

    static UIViewController *controller = nil;
    .....
    {
    .....
    otherWinController = [[NotificationWindowController alloc] init];   
    controller = otherWinController;
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]; //set timer with one second interval
    .....
    }
    
    - (void) timerTick:(NSTimer*)theTimer {
        static int i = 0;
        [controller showMessage:[NSString stringWithFormat:@"%d", i]];
        NSLog(@"%d", i);
        if (++i == 10) {
            [theTimer invalidate];
            i = 0;
        }
    }
    

提交回复
热议问题