I just wrote a iOS app to test the UIAlertView. When I ran it, the UIAlertView just appear with the screen went dark first without the UIAlertView, and "a long time" later ,the UIAlertView appeared. This happens on both Simulator and iPhone (IOS 4.2). I don't know why, Please help me, thanks.
Description:(You can also download the Project HERE)
Its a very simple View based app With 3 Classes: AppDelgate ViewControler and a TestOperation which implement NSOperation; AppDelegate was just the one produced by XCode; TestOperation.h:
#import <Foundation/Foundation.h>
@protocol TestOperationDelegate
- (void)didFinishTestOperaion;
@end
@interface TestOperation : NSOperation {
id <TestOperationDelegate> delegate;
}
@property (nonatomic, assign) id <TestOperationDelegate> delegate;
@end
TestOperation.m
#import "TestOperation.h"
@implementation TestOperation
@synthesize delegate;
- (void)main {
[delegate didFinishTestOperaion];
}
@end
ViewContoller.m
- (void)viewDidLoad {
[super viewDidLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
TestOperation *testOperation = [[TestOperation alloc] init];
testOperation.delegate = self;
[queue addOperation:testOperation];
[testOperation release];
}
- (void)didFinishTestOperaion {
NSLog(@"start uialertview");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Oops!" message:@"Here's the UIAlertView"
delegate:self cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
[alert show];
[alert release];
}
//Solved!! Use performSelectorOnMainThread to make the UI run on Main Thread
Solved!! Use performSelectorOnMainThread
to make the UI run on Main Thread
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];