UIAlertView显示得很慢



我刚刚写了一个iOS应用程序来测试UIAlertView。当我运行它时,UIAlertView只是出现,屏幕首先变暗,没有UIAlertView,"很长一段时间"之后,UIAlertView出现了。这在模拟器和iPhone (IOS 4.2)上都会发生。我不知道为什么,请帮帮我,谢谢。

说明:(你也可以在这里下载项目)

它是一个非常简单的基于视图的应用程序,有3个类:AppDelgate viewcontroller和一个实现NSOperation的TestOperation;AppDelegate是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];
}

//解决! !使用performSelectorOnMainThreadUI运行在主线程

解决了!!使用performSelectorOnMainThread使UI在主线程上运行

[alert performSelectorOnMainThread:@selector(show) withthobject:nil waitUntilDone:NO];

你想在UIAlertView中测试什么?如果你只是从viewDidAppear:在你的ViewController中调用UIAlertView, UIAlertView是否像预期的那样快速显示?

我希望你所遇到的问题与你如何调用UIAlertView有关,并且UIAlertView在你的ViewController控制的UIView出现之前被显示。

最新更新