-3

I would like to pass some variables from my first controller:

float user_distance;
UIImage *user;
float user_battery;
NSString *user_name;

To a second controller. The connection between the two is made by the function:

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

in this manner:

    UserViewController *secondViewController = [[UserViewController alloc] initWithNibName:@"UserViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];

How is it possible to recover the values in from this function?

Thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You manage the dataSource, so from the `indexPath`, you can retrieve the value of your (array?) dataSource. You may now set theses values to `secondViewController`. – Larme May 02 '17 at 15:43
  • how can I retrieve the value of my first variable for example from the indexpath? –  May 02 '17 at 15:55
  • It depends on how you code `tableView:cellForRowAtIndexPath:` – Larme May 02 '17 at 15:57
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Sean Lintern May 02 '17 at 16:02
  • `secondViewController.device = [self.arrayOfDevices objectAtIndex:indexPath.row]` Then? With `UserViewController` having a property `device` of class `SEC_Device`. – Larme May 02 '17 at 16:16

1 Answers1

1

Add the properties you need to your second controller in your .h file ..

//SecondController.h    
@interface SecondController : UIViewController
@property (nonatomic) float *property1;
@property (nonatomic, strong) UIImage *property2;
@property (nonatomic) float *property3;
@property (nonatomic, strong) NSString *property4;
@end

Then import your second controller .h file in your first controller .m file.. And set the properties before pushing the second controller.

//FirstController.m
#import "SecondController.h"
...
...
UserViewController *secondViewController = [[UserViewController alloc] initWithNibName:@"UserViewController" bundle:nil];

secondViewController.property1 = ;//your value here
secondViewController.property2 = ;//your value here
secondViewController.property3 = ;//your value here
secondViewController.property4 = ;//your value here

[self.navigationController pushViewController:secondViewController animated:YES];
  • Yes that's good thanks ! there is just a little mistake, in fact there is no " * " for the declaration of the float ! –  May 03 '17 at 10:17