views:

36

answers:

1

I am playing around with xCode and was trying to create a small app that comprises of a table view (built using an array) comprising the names of various individuals. The user would be able to click on one of the rows in the table view and would be shown a UIAlert with an option to call the person or cancel.

If they click on cancel, the UIalert dismisses. If they click on the Call button, I want to launch the Phone application and dial their number.

Here's what I have in terms of code thus far:

//Generate the UIAlert when the user clicks on a row
- (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 Office"
                                                    message:message
                                                    delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Call",nil];   


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

//Detect which of the UIAlert buttons was clicked and dial a different number
//based on the index of the original row that was selected
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{

    switch (row) 
    {
        case 1:         
            if (buttonIndex != [alert cancelButtonIndex]) 
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://123456789"]];
            }
            break;

        case 2:         
            if (buttonIndex != [alert cancelButtonIndex]) 
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://987654321"]];
            }
            break;

        default:
            break;
    }


}

Here's my question - I know that my switch statement above won't work - That method has no idea what "row" is. So, how exactly do I pass in/access the index of the row that the user tapped for use with my switch statement?

+2  A: 

When you create the UIAlertView right before you say [alert show] set its tag

alert.tag = [indexPath row];

now in your switch(row) statement do

switch(alert.tag)
davydotcom
Oh Jeez! Didn't even think of doing that. Thanks for the note davydotcom. Much appreciated!
vishal