简体   繁体   English

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

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

I have following code for the UIAlertView Loading indicator which is not working and giving me 我有以下代码用于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...
}

Updated: On main thread I am calling web-service and loading data. 更新:在主线程上,我正在调用Web服务并加载数据。 hence I have given another thread for Loading UIAlertView and it was working with iOS 4,5. 因此,我给了另一个用于加载UIAlertView的线程,它正在与iOS 4,5一起工作。 but its crashing in iOS 6. If I am placing AlerView on main thread then while loading nothing shows but after getting data loaded AlertView shows Loading Indicator for few seconds. 但是在iOS 6中崩溃。 Any suggestion... 任何建议...

You are showing the alert from the detached thread while you must do it from the main thread, either use GCD or performSelectorOnMainThread . 您正在显示来自分离线程的警报,而必须从主线程执行警报,请使用GCD或performSelectorOnMainThread


On the main thread you usually want to perform only the UI update, all the complex calculations and data loading are to be executed in the detached threads. 在主线程上,您通常只想执行UI更新,所有复杂的计算和数据加载都将在分离的线程中执行。 If you try to load the data in the main thread, the UI will no be responding during the loading. 如果您尝试将数据加载到主线程中,则UI在加载期间将无响应。 So that is a good practice to load the data in the detached thread, on the main thread you show the alert as you need on the loading start and dismiss it on the loading (and parsing) finished, calling the content UI update as well: 因此,这是将数据加载到分离线程中的一种好习惯,在主线程上,在加载开始时根据需要显示警报,并在加载(和解析)完成后将其关闭,并调用内容UI更新:

加载/刷新数据源流

It's a bad practise. 这是一个坏习惯。

Apple docs say that you need to handle the UI elements on the main thread. 苹果文档说,您需要处理主线程上的UI元素。

I think the issue is with this line: 我认为问题在于此行:

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

You won't handle the UI element on other threads rather than on main thread. 您不会在其他线程而不是主线程上处理UI元素。

Use: 采用:

[self updateFilterProgress];

Or use like: 或使用像:

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

Also I checked with your code. 我也检查了您的代码。 There is one error popping up: 弹出一个错误:

Error is: No visible @interface for 'UIAlertView' declares the selector 'performSelectorOnCurrentThread:withObject:waitUntilDone:' 错误是: “ UIAlertView”没有可见的@interface声明选择器“ performSelectorOnCurrentThread:withObject:waitUntilDone:”

The performSelectorOnMainThread is working perfectly for me. performSelectorOnMainThread对我来说工作完美。

Try this 尝试这个

- (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];

}

You have to call all the UIKit elements in the main thread. 您必须在主线程中调用所有UIKit元素。 That's what the problem is. 这就是问题所在。 Hope this helps. 希望这可以帮助。 Happy Coding. 编码愉快。 :) :)

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

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