0

I am instantiating a detail view controller that exists in storyboard from a different detail view controller and want to give it a data object. In the starting view controller, I use one object (let's call it starting object) that is linked by a relationship to the second data object needed for the second view controller. My question is how can I give the second view controller the second data object? Following code is launching the new VC but without the necessary data:

SecondObject *secondobject;
detailVC *secondViewController =
         [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
         [self.navigationController pushViewController:secondViewController animated:YES];
         secondViewController.startingobject=_startingobject; 

         secondViewController.secondobject = secondobject;
1
  • BTW - please stop putting so many tags in all of your question titles.
    – rmaddy
    Oct 5, 2015 at 15:25

2 Answers 2

3

First of all, try to pass data to your second VC before you push it. This might solve your problem.

4
  • That makes sense. But a problem remains. It seems that I am not getting access to the second object as when I log it to console it is empty. Oct 5, 2015 at 17:19
  • SecondObject *secondobject; - it looks like it is empty :D You have to actually instantiate it.
    – Soberman
    Oct 5, 2015 at 17:22
  • I fixed it by changing to secondViewController.secondobject = _startingobject.secondobject; It seems that this view controller only knows about the other object through the relationship. Your suggestion to pass the objects before launching VC was also needed. Thx! Oct 5, 2015 at 17:27
  • No problems, glad I could help :)
    – Soberman
    Oct 5, 2015 at 18:20
1

Try:

secondViewController.startingobject=_startingobject;

     secondViewController.secondobject = secondobject;

use this code before pushing new viewcontroller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.