在Objective C中点击切换时显示等待消息

时间:2014-02-05 16:45:41

标签: objective-c cocoa-touch

我的用户界面直到加载完成后才更新,因此无法等待等待消息的目的。

- (IBAction)toggle:(UISwitch *)sender {

    [self.waitingMsg setHidden:NO];//UI should update here to show waiting message

    [ThirdViewController scheduleNotifications:[NSDate date]];

    //Unfortunatly UI is not getting updating until the processing.
}

如何在处理开始之前立即更新UI?

2 个答案:

答案 0 :(得分:0)

问题是在代码完成运行之前,实际上并未实际绘制/更新用户界面。所以:

[self.waitingMsg setHidden:NO]; // this is just a command for what WILL happen
[ThirdViewController scheduleNotifications:[NSDate date]]; // but now ...
// ... your code keeps running...
// ... so nothing can happen until AFTER scheduleNotifications returns

解决方案是循环runloop一次,使绘图有机会发生。像这样:

 [self.waitingMsg setHidden:NO];
 dispatch_async(dispatch_get_main_queue(), ^{
    [ThirdViewController scheduleNotifications:[NSDate date]];
    /// ... and all the rest of your code goes here...
 });

答案 1 :(得分:0)

解决了这个问题,请参阅以下代码:

- (IBAction)toggle:(UISwitch *)sender {

    [self.waitingMsg setHidden:NO];//UI should update here to show waiting message

    [self performSelector:@selector(scheduleFromToggle:) withObject:nil afterDelay:1.0];

}


- (void)scheduleFromToggle:(id)sender {

    [ThirdViewController scheduleNotifications:[NSDate date]];

    [self.waitingMsg setHidden:YES];

}