我刚刚编写了一个iOS应用来测试UIAlertView。当我运行它时,UIAlertView只显示屏幕在没有UIAlertView的情况下变暗,并且“很长一段时间”之后,UIAlertView出现了。这在Simulator和iPhone(IOS 4.2)上都会发生。我不知道为什么,请帮助我,谢谢。
描述:(您也可以下载项目HERE)
它是一个非常简单的基于视图的应用程序,有3个类:AppDelgate ViewControler和一个实现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];
}
的 //解决!!使用performSelectorOnMainThread来制作 用户界面在主线程上运行
答案 0 :(得分:8)
解决!!使用performSelectorOnMainThread
使UI在主线程上运行
[alert performSelectorOnMainThread:@selector(show)withObject:nil waitUntilDone:NO];
答案 1 :(得分:1)
你想在UIAlertView中测试什么?如果您只是从viewDidAppear调用UIAlertView:在ViewController中,UIAlertView是否按预期快速显示?
我希望您遇到的问题与您调用UIAlertView的方式有关,并且在您的ViewController控制的UIView出现之前显示UIAlertView。