2012-12-21 40 views
1

我非常有MBProgressHUD和NSURLConnection的挣扎......MBProgressHUD] NSURLConnection的以异步方式

我使用showWhileExecuting方法调用具有内部NSURLConnection的代码另一个私有方法。

-(void)HUD 
{ 
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
    [self.view addSubview:HUD]; 
    HUD.delegate = self; 
    HUD.labelText = @"Initializing"; 

    [HUD showWhileExecuting:@selector(authenticateWithPhoneNumber) onTarget:self withObject:nil animated:YES]; 
} 

这是我通过showWhileExecuting方法调用的方法。

-(void)authenticateWithPhoneNumber 
{ 
    // Do some stuff 
    // Get JSON data using NSURLConnection HERE 

    if(successful) 
    { 
     //EXC_BAD_ACCESS error here 
     [self performSegueWithIdentifier:@"A" sender:self]; 
    } 
    else 
    { 
     //EXC_BAD_ACCESS error here 
     [self performSegueWithIdentifier:@"B" sender:self]; 
    } 
} 

当运行该应用程序,我得到EXC_BAD_ACCESS错误以及以下消息:

28 0x344c0b8b 29 0x344c082b 30 0xf2d39 - [AuthenticationConfirmationViewController authenticateWithPhoneNumber] 31 0xd9013 - [MBProgressHUD launchExecution]

假设那些执行segue代码是问题...我把它们移到HUD方法里面就像这样...

-(void)HUD 
{ 
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
    [self.view addSubview:HUD]; 
    HUD.delegate = self; 
    HUD.labelText = @"Initializing"; 

    [HUD showWhileExecuting:@selector(authenticateWithPhoneNumber) onTarget:self withObject:nil animated:YES]; 

    if(successful) 
    { 
     [self performSegueWithIdentifier:@"A" sender:self]; 
    } 
    else 
    { 
     [self performSegueWithIdentifier:@"B" sender:self]; 
    } 
} 

现在移动到下一个页面是好的......但它跳到甚至JSON数据处理过程之前的某个网页上。

导致所有这些麻烦的问题是什么? 在这种情况下,解决问题的正确方法是什么?在progress.m

+0

在哪里你从视图中删除hud? –

+0

嗯,我使用ARC,并假设我不需要释放它...我必须? – Raccoon

+0

'showWhileExecuting'中有两行保留hud,你是否必须使用'showWhileExecuting'你不能在hud内部使用另一种方法,比如'showHUDAddedTo'? –

回答

1

那么原来的方法做线程的东西,我不是好玩的东西,我不完全理解

#pragma mark - Threading 

- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated { 
    methodForExecution = method; 
    targetForExecution = MB_RETAIN(target); 
    objectForExecution = MB_RETAIN(object); 
    // Launch execution in new thread 
    self.taskInProgress = YES; 
    [NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil]; 
    // Show HUD view 
    [self show:animated]; 
} 

我建议你使用showHUDAddedTo

-(void)authenticateWithPhoneNumber 
    { 
     // Do some stuff 
      // Add right before your JSON executions 
      MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
      hud.labelText = @"Initializing...";  
     // Get JSON data using NSURLConnection HERE 

     if(successful) 
     { 
      // Add at start of requestFinished AND requestFailed basically remove hud just before your segues 
      [MBProgressHUD hideHUDForView:self.view animated:YES]; 
      //EXC_BAD_ACCESS error here 
      [self performSegueWithIdentifier:@"A" sender:self]; 
     } 
     else 
     { 
    // Add at start of requestFinished AND requestFailed basically remove hud just before your segues 
      [MBProgressHUD hideHUDForView:self.view animated:YES]; 
      //EXC_BAD_ACCESS error here 
      [self performSegueWithIdentifier:@"B" sender:self]; 
     } 
    } 
+0

我发现的方法似乎可行,但我也会尝试你的!谢谢空间尘埃 – Raccoon

+0

没问题,我很高兴帮助 –

+2

[HUD showAnimated:YES whileExecutingBlock:^ { [自authenticateWithPhoneNumber] } completionBlock:^ {if(isSuccessfullyAuthenticated) {self performSegueWithIdentifier:@“AuthenticationToMainSegue”sender:self]; } 别的 { [自performSegueWithIdentifier:@ “AuthenticationToListSegue” 发件人:自]; } }]; – Raccoon