Mhmt
Mhmt

Reputation: 769

How to display value of selected row to next view controller?

I have an array.This array loading from web service in TableView.

There are all BranchId in tableview.I want display fields of selected branchId when selected row.

e.g Selected "1234" in tableview

Open new view controller(DetailViewController) :

BranchID:1234

BranchName: ABCDEFGH

I have Branchname in web service

TableviewCodes: http://pastie.org/8052416

How can I display selected ID's detail on new view controller ? Thanks

Upvotes: 0

Views: 1544

Answers (2)

DrDev
DrDev

Reputation: 442

There are different ways, here is one: From your first viewController:

 NSDictionary* dict = [NSDictionary dictionaryWithObject:
                          [NSNumber numberWithInt:theIdYouWantToSend]
                                                     forKey:@"index"];

[[NSNotificationCenter defaultCenter] postNotificationName: @"getID" object: dict];

Now from the new view controller (detailViewController), in the viewDiDLoad method:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getID:) name:@"getID" object:nil];

and create method:

-(void)getID:(NSNotification*)notification{
    NSDictionary* dict = (NSDictionary*) notification.object;

}

You can easily get the ID from the dictionary

myId = [dict objectForKey:@"index"];

Upvotes: 2

Apurv
Apurv

Reputation: 17186

you should modify didSelectRow method by below way:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

     DetailViewController *second=[[DetailViewController alloc] 
initWithNibName:@"DetailViewController" bundle:nil] ;

    second.branchId = [myArray objectAtIndex:indexPath.row];

    [self presentModalViewController:second animated:YES];
}

Upvotes: 1

Related Questions