开发者

Trouble setting UIButton to hidden from within an IBAction

Okay, I have this code in my ViewController.m:

@i开发者_开发知识库mplementation ViewController

@synthesize generateButton = _generateButton;
@synthesize activityIndicator = _activityIndicator;
@synthesize doneLabel = _doneLabel;

// ...

- (IBAction)buttonPressed
{
    // show activity indicator
    _generateButton.hidden = YES;
    _activityIndicator.hidden = NO;

    NSLog(@"processing starting...");

    // do processor heavy work

    NSLog(@"processing done!");

    // display done message
    _activityIndicator.hidden = YES;
    _doneLabel.hidden = NO;
}

// ...

@end

activityIndicator and doneLabel are both set to hidden in interface builder. -(IBAction)buttonPressed is hooked up to generateButton on the Touch Up Inside event.

The trouble is that the button doesn't hide whilst the processor works away. It just keeps the default blue pressed state visible until it's finished working, then shows the doneLabel.


That is because your code is performing the whole process in the same thread.

That thread is blocked until "processor heavy work" is finished

You should perform that "heavy work" in a separate thread and set the state of the button and activity indicator after that separate thread has finished

You can use either NSThread to create a new thread to perform the task or call performSelectorInBackground:withObject:

Either ways, take a look to the Threading Guide in Apple Dev Center

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜