views:

18

answers:

1

I am playing around with UIAlertView and am trying to simply display an NSLog message if the user clicks on the cancel button. I just can't figure out why absolutely nothing is happening.

Here's the code that I have in place:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexpath
{
    NSUInteger row = [indexpath row];
    NSString *rowValue = [listData objectAtIndex:row];

    NSString *message = [[NSString alloc] initWithFormat:@"Contact %@", rowValue];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Call this person"
                                                    message:message
                                                    delegate:nil
                                                    cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Call",nil];


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

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{

        if (buttonIndex == [alert cancelButtonIndex]) 
        {

               //Just a quick write to the console
               NSLog(@"You clicked on the cancel button");
               //Initially tried to see if I could dial a number            
               //[[UIApplication sharedApplication] 
               //openURL:[NSURL URLWithString:@"tel://123456789"]];
        }   

}

I know i am missing something really obvious! Any suggestions?

Also, on a related note - This little test app is actually a table view with 10 entries populated from an Array. What I'd like to do is be able to use the array index and display a different message for each item. Given my setup below, how would I reference the tableview row in my alertView:clickedButtonAtIndex method.

+1  A: 

you need to set the alert view delegate to self for a start.

then you need to define that the class you are in is the alert view delegate.

@interface YourCustomViewClass : UIViewController <UIAlertViewDelegate> {

}

@end
Thomas Clayson
Thomas - Thanks so much for your note! I did have my class also set up to be the delegate. What i completely overlooked, was that the alert view's delegate property was set to nil!
vishal