Programming UIViewControllers cycles with ios and

2019-06-01 18:26发布

问题:

I am just starting ios development with IOS 5 and storyboards and would like to know how I can solve the following problem:

I have a tree hierarchy with categories an products being nodes of the tree and products are leaves. I created a TileViewController view to display all nodes at a specific level. When the user selects a node in the TileView, depending on its type (leaf or not-leaf) I either push a new TileViewController into the application NavigationController or I show a DetailViewController.

What I do currently is to do everything programmatically(By that I mean tracking what the user clicked on in the TileViewController and decide what to push next in the NavigationController). I get a handle on the storyboard and use the below code to create the next TileViewController if needed.

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        TileViewController *controller =  [storyBoard instantiateViewControllerWithIdentifier:@"TileViewController"];
        controller.model = self.model;   
        [self.navigationController pushViewController:controller animated:YES];

What I can't figure out is how to make this using a storyboard instead of hardcoding logic in the TileViewController. Is this at all possible? I thought about using a single TileViewController and just reloading the tiles displayed based on the user selection but then I lose the navigation piece. Any ideas how I could achieve this?

Thank you.

回答1:

You can define a segue from whatever UI component kicks off the navigation, back to the same scene in the storyboard - so if it was from a button, ctrl-drag from the button down to the yellow sphere representing the tile view controller.

Define this as a push segue, then pass the relevant detail in prepareForSegue. This will create a potentially infinite navigation stack without having to repeat yourself in the storyboard.