fundidor
fundidor

Reputation: 207

Retrieving tapped button on UIAlertView

I had this code running on my app, and now on iOS 6 it crashes my app:

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

I have learned that calling the alert from a second thread causes the crash, and so I changed my code to call the alert from the main thread like this:

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

Note I am parsing the title, message and buttons as a list to 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];
    }
}

My problem now, is that I had this handler to catch the button that was tapped, and it doesn't work any more. It just isn't called:

- (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...
    }
}

Upvotes: 1

Views: 442

Answers (2)

NSGod
NSGod

Reputation: 22948

The problem is that in your re-written method, you set the delegate to nil instead of self. As a result, the alertView:clickedButtonAtIndex: delegate method isn't called. Change your code to the following: by specifying self in the delegate: parameter:

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

Upvotes: 2

J2theC
J2theC

Reputation: 4452

You removed the delegate from the creation method of your UIAlertView

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

should be

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

Upvotes: 2

Related Questions