Reputation: 13
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.
Upvotes: 1
Views: 614
Reputation: 3677
@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];
}
}
Upvotes: 0
Reputation: 179
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/
Upvotes: 2
Reputation: 26383
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:
-selectedData
Upvotes: 1
Reputation:
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];
}
Upvotes: 1