1

我在我的应用程序上运行了这段代码,现在在 iOS 6 上它使我的应用程序崩溃:

// Another method
[NSThread detachNewThreadSelector:@selector(askServerForNFeID)
          toTarget:self withObject:nil];

- (void)askServerForNFeID {
    if ([response isEqualToString:@"XXXX"]) {
        NSString *responseMessage = [NSString stringWithFormat:
                                       NSLocalizedString(@"Autorizado o Uso da NFe\n para chave:\n%@", @""),
                                       [invoiceInfo NFeID]];

        UIAlertView *alert = [[UIAlertView alloc]
               initWithTitle:NSLocalizedString(@"Sefaz respondeu:", @"")
                     message:responseMessage
                    delegate:self
           cancelButtonTitle:NSLocalizedString(@"OK", @"")
           otherButtonTitles:nil];

        [alert show];
        [alert release];
    }
}

我了解到从第二个线程调用警报会导致崩溃,因此我将代码更改为从主线程调用警报,如下所示:

if ([response isEqualToString:@"XXXX"]) {

    [self performSelectorOnMainThread:@selector(showAlertHelper:)
          withObject:[[NSArray alloc] initWithObjects:
            NSLocalizedString(@"Não posso seguir em frente", @""),
            NSLocalizedString(@"Você usou .....", @""), @"Fechar", @"Comprar", nil]
            waitUntilDone:YES];

注意我正在将标题、消息和按钮解析为 showAlertHelper 的列表...

-(void)showAlertHelper:(NSArray*)theArray{

    if ([[theArray objectAtIndex:3] isEqualToString:@""]) {
        UIAlertView *alertView = [[UIAlertView alloc]
              initWithTitle:[theArray objectAtIndex:0]
                    message:[theArray objectAtIndex:1]
                   delegate:nil
          cancelButtonTitle:[theArray objectAtIndex:2]
          otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    else {
        UIAlertView *alertView = [[UIAlertView alloc]
             initWithTitle:[theArray objectAtIndex:0]
                   message:[theArray objectAtIndex:1]
                  delegate:nil
         cancelButtonTitle:[theArray objectAtIndex:2]
         otherButtonTitles:[theArray objectAtIndex:3], nil];

        [alertView show];
        [alertView release];
    }
}

我现在的问题是,我有这个处理程序来捕捉被点击的按钮,它不再起作用了。它只是不被称为:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:NSLocalizedString(@"Comprar", @"")]) {
        // Do stuff to buy credits...
    } else if ([title isEqualToString:NSLocalizedString(@"Fechar", @"")]) {
        NSLog(@"Fechar was selected.");
    }
    else if ([title isEqualToString:NSLocalizedString(@"Criar conta", @"")]) {
         // Do stuff to create an account...
    }
}
4

2 回答 2

2

您从 UIAlertView 的创建方法中删除了委托

UIAlertView *alertView = [[UIAlertView alloc]
         initWithTitle:[theArray objectAtIndex:0]
               message:[theArray objectAtIndex:1]
              delegate:nil
     cancelButtonTitle:[theArray objectAtIndex:2]
     otherButtonTitles:[theArray objectAtIndex:3], nil];

应该

UIAlertView *alertView = [[UIAlertView alloc]
         initWithTitle:[theArray objectAtIndex:0]
               message:[theArray objectAtIndex:1]
              delegate:self
     cancelButtonTitle:[theArray objectAtIndex:2]
     otherButtonTitles:[theArray objectAtIndex:3], nil];
于 2012-09-21T21:10:46.823 回答
2

问题是在您重写的方法中,您将委托设置为nil而不是self. 因此,alertView:clickedButtonAtIndex:不会调用委托方法。将您的代码更改为以下内容:通过selfdelegate:参数中指定:

-(void)showAlertHelper:(NSArray*)theArray{

    if ([[theArray objectAtIndex:3] isEqualToString:@""]) {
        UIAlertView *alertView = [[UIAlertView alloc]
              initWithTitle:[theArray objectAtIndex:0]
                    message:[theArray objectAtIndex:1]
                   delegate:self
          cancelButtonTitle:[theArray objectAtIndex:2]
          otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc]
             initWithTitle:[theArray objectAtIndex:0]
                   message:[theArray objectAtIndex:1]
                  delegate:self
         cancelButtonTitle:[theArray objectAtIndex:2]
         otherButtonTitles:[theArray objectAtIndex:3], nil];

        [alertView show];
        [alertView release];
    }
}
于 2012-09-21T21:10:55.147 回答