UIAlertView showing up very slowly
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];
What are you attempting to test in the UIAlertView? If you simply called the UIAlertView from viewDidAppear: in your ViewController, is the UIAlertView displayed rapidly as expected?
I expect that the issues you are having are related to how you are calling the UIAlertView, and that the UIAlertView is being displayed before the UIView controlled by your ViewController has appeared.
精彩评论