i'm trying to pass the data of the tableviewcell to the destination ViewController where i have a label and i want to set that data to label's text using segue when i select the particular cell.
            Asked
            
        
        
            Active
            
        
            Viewed 611 times
        
    1
            
            
        - 
                    2Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – vadian Jun 18 '16 at 18:52
4 Answers
2
            
            
        You can use below method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SegueName"]) {
        //Create Object/Variable in destination ViewController and assign here 
    } }
You can check the Demo @ http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
 
    
    
        Hardik Shah
        
- 161
- 1
- 10
1
            Simply declare a global variable in your origin view controller's .m file to store the data, and a property of the same type in your destination view controller, then store that data in the property in your -prepareForSegue method.
Your code should look like this:
@implementation OriginViewController
{
  ObjectType *object;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    object = // whatever value you want to store
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue
{
    DestinationViewController *destVC = [segue destinationViewController];
    [destVC setProperty:object];
}
1
            
            
        It really simple:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:<#whatever#>]) {
        NSIndexPath * selectedIndexPath = [tableView indexPathForSelectedRow];
        // get from your data source the data you need at the index path
        YourVC * destVC = [segue destinationViewController];
        destVC.selectedData = data;
    } 
}
This means:
- Your table view has selection enable
- Your destination view controller has property -selectedData
- You have a segue that starts from the prototype table view cell to your destination view controller
 
    
    
        Andrea
        
- 26,120
- 10
- 85
- 131
0
            
            
        @property (strong, nonatomic) NSIndexPath *selectedRow; // set a default value of nil in viewDidLoad method
Now just use this logic to pass data from UITableView to any UIViewController
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    selectedRow = indexPath;
    //now simply invoke this method to perform segue.
    [self performSegueWithIdentifier:@"Your-Identifier" sender:self];
}
check your segue identifier and then pass the data.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"Your-Identifier"]) {
            UIViewController *viewController = segue.destinationViewController;
            //Your current selected cell path is stored in selectedRow if you have save value in any Array you can fetch it like this...
            viewController.firstName = [arrayOfFirstNames objectAtIndex:selectedRow.row];
            //Or You can access the tableView cell by this method.
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedRow];
        }
}
 
    
    
        Syed Qamar Abbas
        
- 3,637
- 1
- 28
- 52
