将UIAlertView放在UIActivityIndi​​catorView上方

时间:2011-11-16 17:14:36

标签: objective-c view uialertview uiactivityindicatorview

我想看看我的UIAlert在我的UIActivityIndi​​catorView之上,我有这个代码:

- (void)showWithLabelDeterminate{

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    // Set determinate mode
    HUD.mode = MBProgressHUDModeDeterminate;

    HUD.delegate = self;
    HUD.labelText = @"Loading";

    // myProgressTask uses the HUD instance to update progress
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
}

- (void)myProgressTask
{
    float progress = 0.0f;

    while (progress < 0.99f) 
    {
        progress += 0.01f;
        HUD.progress = progress;
        usleep(50000);

        if(progress > 0.02f & progress < 0.05f)  
        {
            NSString * string = [NSString stringWithFormat:@"http://localhost:8080/......"];
            NSURL *url = [NSURL URLWithString:string]; 

            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                       timeoutInterval:60.0];

           urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

           if(!urlData) 
           {
                [self simpleAlert];

           }
           else
           {
                   datos = [[NSMutableString alloc]initWithData:urlData  encoding:NSISOLatin1StringEncoding];
           }
       }
    }
}

-(void)simpleAlert 
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Dont connect!!"   delegate:self cancelButtonTitle:@"Refresh" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

问题是我在UIActivityIndi​​catorView上面看不到我的UIAlertView .....

1 个答案:

答案 0 :(得分:1)

myProgressTask要么在后台线程上执行,要么通过调用usleep来阻止主线程。

如果它在后台线程上,则使用UIAlertView是无效的,因为在一般情况下,UIKit只能从主线程使用。最快的解决方案是使用performSelectorOnMainThread:...来呼叫simpleAlert

如果它在主线程上,那么你可能会阻止UIKit运行。关于使用非阻止NSURLConnection并使用NSTimerperformSelector:withObject:afterDelay:来安排暂停后发生的事情,将会有明显的评论。