无法将UIButton设置为在IBAction中隐藏

时间:2011-05-03 10:39:18

标签: iphone objective-c uibutton hidden

好的,我在ViewController.m

中有这段代码
@implementation 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

activityIndicatordoneLabel都设置为隐藏在界面构建器中。 -(IBAction)buttonPressed事件generateButtonTouch Up Inside联系在一起。

问题是当处理器工作时按钮不会隐藏。它只保持默认的蓝色按下状态,直到它完成工作,然后显示doneLabel

1 个答案:

答案 0 :(得分:4)

这是因为您的代码在同一个线程中执行整个过程。

该线程被阻止,直到“处理器繁重工作”完成

您应该在单独的线程中执行“繁重的工作”,并在单独的线程完成后设置按钮和活动指示器的状态

您可以使用 NSThread 创建新线程来执行任务或调用 performSelectorInBackground:withObject:

无论哪种方式,请查看Apple开发人员中心的Threading Guide

相关问题