views:

93

answers:

0

I have an item on toolbar for user to start a job. When the job is running, I want to change the item to an active indicator. After the job finished, the item switch back to normal status. I found the item will disappear if the job finish too fast.

My test code:

TestToolbarViewController.h

@interface TestToolbarViewController : UIViewController {
    UIBarButtonItem *refreshItem, *indicatorItem;
    UIToolbar *toolbar;
}

@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;

@end

TestToolbarViewController.m

@implementation TestToolbarViewController

@synthesize toolbar;

- (void)viewDidLoad {
    [super viewDidLoad];

    refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(working)];
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    indicatorItem = [[UIBarButtonItem alloc] initWithCustomView:indicator];
    [indicator startAnimating];
    [indicator release];

    [toolbar setItems:[NSArray arrayWithObject:refreshItem] animated:YES];
}

- (void)working {
    [toolbar setItems:[NSArray arrayWithObject:indicatorItem] animated:YES];
    [self performSelector:@selector(done) withObject:nil afterDelay:0.1];
}

- (void)done {
    [toolbar setItems:[NSArray arrayWithObject:refreshItem] animated:YES];
}


- (void)dealloc {
    [refreshItem release];
    [indicatorItem release];
    [toolbar release];
    [super dealloc];
}

@end

If delay in working method is bigger, for example 0.5 second, or animated = NO, everything will be ok. Any solution except increase delay manually?