Reputation: 5101
I have a table view with custom cells. On the cell there are buttons identified with tags. Every button performs an action. As example:
-(void)mapa_button_action:(UIButton*)sender
{
NSLog(@"CANCELAR BUTTON CLICKED=%ld",(long)sender.tag);
NSString *employee =[[historialServicios objectAtIndex:sender.tag] valueForKey:@"driverId"];
NSLog(@"EMPLOYEE=%@",employee);
[self performSegueWithIdentifier:@"mapa_conductor_segue" sender:self];
}
As you can see, the button is identified with the sender.tag property. In this case, the action performs a segue, and later, it performs the prepareForSegue method, where I can pass variables to the new view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"mapa_conductor_segue"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"INDEXPATH=%@",indexPath);
}
}
What I need is to retrieve the values from the selected cell to the new view controller. I am able to get the values in the map_button_action method, but I don't know how to pass these values to the new controller.
I must use the performSegueWithIdentifier action in order to maintain the view controllers structure, I am using SWRevealViewControllers.
Any help is welcome.
Upvotes: 0
Views: 724
Reputation: 1661
In the destination ViewController
create a property(of the type you need to pass data in). You can create an object of the destination controller in your current ViewController
and set the value of this object in the map_button_action
method to do what you want.
//DestinationViewController.h
@interface DestinationViewController : UIViewController
@property (nonatomic,strong) (SomeDataType*) propertyName;
@end
//CurrentViewController.m
#import DestinationViewController
....
map_button_action{
DestinationViewController *obj = [DestinationViewController new];
obj.propertyName = someValue;
[self.navigationController pushViewController:obj animated:YES]
}
Upvotes: 1
Reputation: 104082
You're passing a useless argument as the sender when you call performSegueWithIdentifier:sender. If you want the button's tag to be available in prepareForSegue, then set that tag (converted to an NSNumber) as the sender -- you can pass any object you want in that parameter, and it will be the sender in prepareForSegue.
-(void)mapa_button_action:(UIButton*)sender
{
NSLog(@"CANCELAR BUTTON CLICKED=%ld",(long)sender.tag);
NSString *employee =[[historialServicios objectAtIndex:sender.tag] valueForKey:@"driverId"];
NSLog(@"EMPLOYEE=%@",employee);
[self performSegueWithIdentifier:@"mapa_conductor_segue" sender:@(sender.tag)];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSNumber *)sender {
NSInteger tag = sender.integerValue;
// do whatever you need to with the tag
if ([segue.identifier isEqualToString:@"mapa_conductor_segue"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"INDEXPATH=%@",indexPath);
}
}
Upvotes: 3
Reputation: 3393
try this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"mapa_conductor_segue"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"INDEXPATH=%@",indexPath);
DestinationViewController *objeDest=segue.destinationViewController;
objeDest.destinationIndexPath=indexPath
}
}
Upvotes: 1