Поместите UIAlertView выше UIActivityIndicatorView - PullRequest
0 голосов
/ 16 ноября 2011

Я хочу через мгновение увидеть мой UIAlert над моим UIActivityIndicatorView, у меня есть этот код:

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

Проблема в том, что я не вижу свой UIAlertView над моим UIActivityIndicatorView .....

1 Ответ

1 голос
/ 16 ноября 2011

myProgressTask либо выполняется в фоновом потоке, либо блокирует основной поток своими вызовами usleep.

Если он находится в фоновом потоке, то использование UIAlertView недопустимо, поскольку в общем случае UIKit можно использовать только из основного потока. Самым быстрым решением было бы использовать performSelectorOnMainThread:... для вызова simpleAlert.

Если он находится в главном потоке, вы, вероятно, блокируете работу UIKit. Далее следуют очевидные комментарии об использовании неблокирующего NSURLConnection и использовании NSTimer или performSelector:withObject:afterDelay: для планирования событий, которые должны произойти после паузы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...