1

I have two UIViewControllers: TimerViewController and EfficiencyViewController (For simplicity's sake, we will call them TVC and EVC)

I am trying to pass certain values (2 NSString objects, 1 NSTimeInterval)from TVC to EVC when a button is pressed. EVC needs to be intialized and pop up upon pressing the button. Overall, I have tried two methods.

1. Directly passing the values (In TVC)

     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
EfficiencyViewController *efficiencyViewController = [storyboard instantiateViewControllerWithIdentifier:@"EfficiencyView"];
efficiencyViewController.category = _categoryLabel.text;
efficiencyViewController.desc = _descriptionTextField.text;
efficiencyViewController.duration = [_timer getInterval];
efficiencyViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:efficiencyViewController animated:YES completion:NULL];

Problem: When I instantiate EVC, the values I held in TVC are reset, so basically no data is passed. (I think this is because EVC actually pops up on the screen)

2. Building a custom init method

TVC

EfficiencyViewController *efficiencyViewController = [[EfficiencyViewController alloc] initWithName:_categoryLabel.text desc:_descriptionTextField.text duration:[_timer getInterval]];
[self presentViewController:efficiencyViewController animated:YES completion:NULL];

EVC initWithName method implementation

- (id)initWithName:(NSString *)category desc:(NSString *)theDesc duration:(NSTimeInterval)theDuration {
// self = [super initWithNibName:@"EfficiencyViewController" bundle:nil];
if (self != nil) {
    _category = category;
    _desc = theDesc;
    _duration = theDuration;
}
return self;
}

Problem: The values are simply not being passed. And also in this way, EVC is missing some major components, such as a button and a text label.

halfer
  • 19,824
  • 17
  • 99
  • 186
besnuj
  • 21
  • 3
  • I'm not sure that [Passing Data between View Controllers](http://stackoverflow.com/q/5210535/643383) is exactly a duplicate, but it should help. Half your problem seems to stem from from instantiating your view controllers yourself. It's possible to do that of course, but you're swimming upstream. @NRitH's suggestion to use segues is the preferred route. – Caleb Feb 13 '17 at 21:20

2 Answers2

1

If you have these in a storyboard, you should be using a storyboard segue, and in TVC's prepareForSegue(), you get the destination view controller (i.e. EVC) from the segue object, and that's where you set EVC's properties.

NRitH
  • 13,441
  • 4
  • 41
  • 44
0
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"
 bundle:nil]; EfficiencyViewController *efficiencyViewController =
 [storyboard
instantiateViewControllerWithIdentifier:@"EfficiencyView"];
efficiencyViewController.category = _categoryLabel.text;
efficiencyViewController.desc = _descriptionTextField.text;
efficiencyViewController.duration = [_timer getInterval];
efficiencyViewController.modalTransitionStyle =
 UIModalTransitionStyleCoverVertical;

Use self instead of _, see this link https://stackoverflow.com/a/30901681/4912468

Community
  • 1
  • 1
  • Hm.. I see. The reason I chose to use _, is because of readability. Wouldn't using self. hinder the readability of your code? What's your opinion? – besnuj Feb 15 '17 at 04:24