InstantiateViewControllerWithIdentifier and transfer data

I use Storyboard in my application and I want to transfer data from one view to another.

Instead of using segues, I use instantiateViewControllerWithIdentifier . In this case, I create an instance from my first TableViewController in the NavigationController , which has a second bound TableViewController , because I need navigation in the second TableViewController . Now I want to transfer data from my first TableViewController , depending on which row was clicked, to my second TableViewController . In this case, newTopViewController will be my NavigationController , but now my problem is how to transfer data from firstTableViewController to secondTableviewController .

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = [NSString stringWithFormat:@"%@Top", [menuArray objectAtIndex:indexPath.row]]; UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier]; } 

enter image description here

+6
source share
4 answers

If you create an instance of navigationController, you can use the viewControllers property to get the internal viewController of the navigation controller.

Something like that:

 UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:identifier]; MBFancyViewController *viewController = navigationController.viewControllers[0]; // setup "inner" view controller viewController.foo = bar; [self presentViewController:navigationController animated:YES completion:nil]; 
+14
source
 newTopViewController.anyVariableToShow= anyVariableToSend; 

I do this quite often on several of my applications ...

// Create a new VC

 CookViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CookVC"]; //Set recipe [detailViewController setRecipe:recipe]; //Pop over VC (can be pushed with a nav controller) [self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationFade]; 

If you are not using a navigation controller or segues, then I think you need to rethink the design of your application.

+5
source

In fact, this is not just a data transfer problem, as it is a matter of managing the program and transferring the data together. Even you will have to rethink the idea of ​​your application, since you want to use the storyboard without the meaning of the storyboard, it is up to you, and I hope that you have every reason to do what you do.

Therefore, when you decided not to use segue, you lost a new and convenient way to create a new controller and transfer data with it, and you need to transfer control and data in two different steps. When you instantiate another scene in the storyboard (as with instantiateViewControllerWithIdentifier :), you simply created a new controller and passed the control, but not the data. Think about it when you instantiated a new controller from xib in the old way (so you should use initWithCoder: or awakeFromNib in the second view controller, since the storyboard will not call initWithName: bundle :), but it didn’t do anything else.

Thus, you will have a new controller (it is called in the personal section of the second storyboard) that hangs in the universe without any relationship or connection with anything else (as the image in the storyboard mode illustrates well), and you can do it what you would like.

So, you want to do something, and you need the data from the previous storyboard (ViewController). You need to provide this data in the second storyboard (ViewController), and, as you know, there are many solutions for this that were available a long time before there was even a storyboard.

So, regarding your code, “data transfer” depends on your design, whether the two controllers are subclasses of each other or even ...

If you don’t like dealing with a subclass and decoupling them as much as possible, the best way is to simply make the property of your data in the first controller and reference them from the second (after importing the first. H file) and just refer to it in the viewDidLoad file or in initWithCoder : or somewhere where you need them, as

 secondViewControllerdata = firstViewControllerdata.thatDataProperty 

Of course, you can do the same thing in reverse order and create a property of the second controller and access it in your first view controller.

0
source

You can define some parameter in the UIViewController to get the data:

@property (assign) int param1;

@property (save) NSMutableArray * param2;

and use below to transfer data:

[newTopViewController setParam1: XX];

[newTopViewController setParam2: XX];

0
source

All Articles