I’m trying to change the enabled property of an UIBarButtonItem after doing some stuff in an NSThread. After pressing the button I set the enabled to NO then perform the threaded part and at the end I try to re enable the button. Fairly basic.
Somehow this fails, however I’m able to change any other property of the the UIBarButtonItem correctly (for ex. title).
What am I doing wrong here?
@interface myViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UIBarButtonItem *myButton;
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *myButton;
- (IBAction)mysub:(id)sender;
@end
@implementation myViewController
@synthesize myButton;
- (IBAction)mysub:(id)sender {
[myButton setEnabled:NO];
[NSThread detachNewThreadSelector:@selector(mysub_threaded) toTarget:self withObject:nil];
}
- (void) mysub_threaded {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
… do threaded stuff
[myButton performSelectorInBackground: @ selector(setEnabled :) withObject: [NSNumber numberWithBool:YES]];
[pool drain];
}
You want
performSelectorOnMainThreadinstead.Always do anything that touches the UI on the main thread.
But sometimes passing arguments like this is funky too. I find it best to wrap up everything you need done in another method
Then call that with
Now you can do as much other UI stuff as you want without worry about it.