繁体   English   中英

UIAlertView加载指示器在辅助线程中不起作用

[英]UIAlertView Loading indicator doesn't work in a secondary thread

我有以下代码用于UIAlertView Loading指示器,该指示器不起作用并给我

- (void) launchActivity
{
 //some logic...
[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];
}
- (void) updateFilterProgress {
 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connectivity" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
    [myAlert show];
}
else{
    UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease] ;


  //tried this way by placing below line....no result   
  [alertMe performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
    //[alertMe show];

 //some logic...
}

更新:在主线程上,我正在调用Web服务并加载数据。 因此,我给了另一个用于加载UIAlertView的线程,它正在与iOS 4,5一起工作。 但是在iOS 6中崩溃。 任何建议...

您正在显示来自分离线程的警报,而必须从主线程执行警报,请使用GCD或performSelectorOnMainThread


在主线程上,您通常只想执行UI更新,所有复杂的计算和数据加载都将在分离的线程中执行。 如果您尝试将数据加载到主线程中,则UI在加载期间将无响应。 因此,这是将数据加载到分离线程中的一种好习惯,在主线程上,在加载开始时根据需要显示警报,并在加载(和解析)完成后将其关闭,并调用内容UI更新:

加载/刷新数据源流

这是一个坏习惯。

苹果文档说,您需要处理主线程上的UI元素。

我认为问题在于此行:

[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];

您不会在其他线程而不是主线程上处理UI元素。

采用:

[self updateFilterProgress];

或使用像:

[yourAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

我也检查了您的代码。 弹出一个错误:

错误是: “ UIAlertView”没有可见的@interface声明选择器“ performSelectorOnCurrentThread:withObject:waitUntilDone:”

performSelectorOnMainThread对我来说工作完美。

尝试这个

- (void) launchActivity
{
 //some logic...
[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];
}
- (void) updateFilterProgress {
 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
    [self performSelectorOnMainThread: @selector(showAlertForNoNetConnect)];
}
else{
    [self performSelectorOnMainThread: @selector(showAlertForLoading)];
 //some logic...
}

- (void) showAlertForNoNetConnect
{
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connectivity" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
    [myAlert show];

}
- (void) showAlertForLoading
{
    UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease] ;
    [alertMe show];

}

您必须在主线程中调用所有UIKit元素。 这就是问题所在。 希望这可以帮助。 编码愉快。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM