1

我在 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 数据处理过程结束之前就跳转到其中一个页面。

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

4

1 回答 1

1

正在进行中的原始方法。m 必须对线程做一些事情,我对我不完全理解的事情不感兴趣

#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];
        }
    }
于 2012-12-21T19:29:47.103 回答