How to pass variable from uitableview to second tableview using didSelectRowAtIndexPa

Discussion in 'iOS Programming' started by EliasRahme, Aug 8, 2012.

  1. EliasRahme macrumors newbie

    Jul 18, 2012
    i want to pass the variable from a view controller to another one, so that when the user selects a certain row in the first table view, the application will take him to another view controller in which the details of the selected item will appear.
    This is my code :
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        	NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row];
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
            Details *dvController = [storyboard instantiateViewControllerWithIdentifier:@"Details"]; //Or whatever identifier you have defined in your storyboard
            dvController.selectedAuthors = selectedAuthors;
            UIAlertView *messageAlert = [[UIAlertView alloc]
                                        initWithTitle:@"Row Selected" message:authorNAme delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
           // Display Alert Message
            authorNAme =  [[NSString stringWithFormat:[theauthors objectAtIndex:indexPath.row]] intValue];
         [messageAlert show];
        	[self.navigationController pushViewController:dvController animated:YES];
    selectedAuthors is a string
    authorName is a global variable in which i want to store the content of the selected row.
    Any help will be highly appreciated.
  2. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia

    Your setup seems reasonable (aside from the fact that global variables are best avoided, since they cause tight coupling between your application classes.)

    It looks like you added a property, selectedAuthors, to your Details view controller, which is a good way to pass data. You're creating a new view controller from your Storyboard, setting a property, and pushing it onto the nav controller. That's a little unconventional, since using segues is the normal way to transition between view controllers in a storyboard, but at a glance it looks like it should work.

    What is and isn't working about your code?

    Do you see the value in selectedAuthors in your Details view controller's viewWillAppear method?

    Note that you should not try to assign your selectedAuthors property to a view until viewDidLoad (or better yet, viewWillAppear), since your views may not be loaded yet.

Share This Page