iPad的-NSThread错误

时间:2011-06-20 11:09:38

标签: ipad

对于检查有效登录, 我从Web服务获取数据并显示进度, 我在UIAlertView中显示UIIndicatorView 问题是: 在进行按下IPad设备的主页按钮然后我的应用程序关闭时,第二次当我尝试启动应用程序然后(黑屏显示)应用程序未启动。 我怎么能解决这个问题?

我的代码是:

-(NSMutableString*) getLoginMessage:(NSString*) UserName : (NSString *) Password : (NSString *) url

{

[NSThread detachNewThreadSelector:@selector(showAlertMethod) toTarget:self withObject:nil];
@try
{
NSArray *Keys =[[NSArray alloc] initWithObjects:@"LoginName",@"PassWord",nil];
NSArray *KeyValue =[[NSArray alloc] initWithObjects:UserName,Password,nil];
operationName=[[NSString alloc] init];
operationName =@"ClientLogin";
NSURL *WebServiceUrl=[WebServiceHelper generateWebServiceHTTPGetURL:url : operationName :     Keys:     KeyValue];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:WebServiceUrl];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser setDelegate:self];
[parser parse];
[Keys release];
[KeyValue release];
[WebServiceUrl release];
[NSThread detachNewThreadSelector:@selector(dismissAlertMethod) toTarget:self withObject:nil];
}
@catch (NSException * e)
{
[NSThread detachNewThreadSelector:@selector(dismissAlertMethod) toTarget:self withObject:nil];
}
return Result;
}

-(void)showAlertMethod
{
NSAutoreleasePool *pool1=[[NSAutoreleasePool alloc]init];
progressAlert = [[UIAlertView alloc] initWithTitle:@"Signing in..." message:@"Please wait..."    delegate:nil cancelButtonTitle: nil otherButtonTitles:nil];

CGRect alertFrame = progressAlert.frame;
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

activityIndicator.frame = CGRectMake(135,alertFrame.size.height+75, alertFrame.size.width,30);
activityIndicator.hidden = NO;
activityIndicator.contentMode = UIViewContentModeCenter;
[activityIndicator startAnimating];
[progressAlert addSubview:activityIndicator];
[activityIndicator release];
[progressAlert show];
[pool1 release];
}

-(void)dismissAlertMethod
{
NSAutoreleasePool *pool2=[[NSAutoreleasePool alloc]init];
[progressAlert dismissWithClickedButtonIndex:0 animated:YES];
[pool2 release];
}

1 个答案:

答案 0 :(得分:0)

有点难以阅读您的代码,但是:您无法从线程中执行UI。必须创建警报并显示主线程。