Reputation: 698
I have following view controller hierarchy with Parent children relationship.
VC1 -- VC2 -- VC3 -- VC4 -- VC5
Each view controller is children of predecessor.
I have used code -
[parentVC addChildViewController:childVC];
[childVC.view setFrame:parentVC.view.frame];
[parentVC.view addSubview:childVC.view];
[childVC didMoveToParentViewController:parentVC];
Now, I want to replace VC1 to another view controller, say VC0 in this hierarchy after an action from VC5 i.e. After removing VC2, I want to have VC0 instead of VC1. How to achieve this?
Upvotes: 3
Views: 1563
Reputation: 119021
Delegation is the appropriate solution, where the source parent (the delegate) is passed down through the chain to the leaf controller (that wants to callback).
The fact that this is unpleasant indicates that what you're trying to do is a bit weird. But, not knowing the requirement limits larger suggestions.
If you want to cheat your way out (you shouldn't) then posting a notification is much less code. Using blocks as the implementation of the delegate pattern is medium code.
Upvotes: 0
Reputation: 124997
UINavigationController
provides a -setViewControllers:animated:
method that you can use to modify the navigation stack. It's usually used to restore an app to the state it was in when the user left the app. From the docs:
Use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.
That said, using this method to replace view controllers in the stack sounds like a poor plan from a UI point of view -- it seems likely to confuse users, who fully expect parent controllers not to transmogrify into something completely different.
Upvotes: 2