How do I switch the ViewController instead of adding to the stack?

I have a series of objects communicating with each other in a circle. Each object has a link to it. Neighbors, square and triangle are different types of classes:

Each object holds a reference to it's neighbors, square and triangle are different class types

The tow types, triangle and square are made visible with the ViewController, and they link to each other using segues.

So far, no problem. However, while I go through my structure, I keep adding ViewControllers to each other. Not only does this seem like bad practice from memory, but it also presents a problem that when I want to exit this structure, I have to go back, closing all the ViewControllers I have opened.

So what I'm looking for is a way to not add the next ViewController on top of the current one on the stack, but replace the current ViewController with the next one.

I've been looking for a while for a solution but haven't had much success. So I feel like doing what I want is either impossible, or I just don't understand and don't know what to look for. Do I need RootViewController for something like this? Or should I create a custom padding that overrides the old ViewController before adding the new one? I'm really at a loss.


source to share

5 answers

Add all subViews once, to viewDidLoad and tag all your SubViews after that, where you want to show that the view in the viewController doesn't add it, just bring it to the front by calling the function [[self.view viewWithTag: 1] BringToFront]



Since you are probably using a navigation controller, you should take a look at the UINavigationController link. There are ways to change the navigation stack. You cannot do this from just a storyboard. You will need a special code.



You should look at the UINavigationController's setViewControllers: animated: method .



It sounds like you want to do one of two things

  • Replace rootViewController

  • Have rootViewController

    one that acts as a container for one UIViewController

    and changes it as needed. In iOS 5, you can do this with a custom UIViewController, but or you could use one which Apple provided.

    UINavigationController`, but unless you also use it to navigate the tree like the view controllers framework, this is probably not the best option.

Your best answer seems to depend on your needs.

If your controllers do not have a lot of states or cannot be replaced, you can use option 1.

If you expect users to change frequently between controllers and / or your controllers require a lot of customization or have a lot of state, then you might want to NSArray

and just use presentViewController:animated:completion:

to show different controllers when needed. Keeping your controllers in NSArray

has the added bonus of making it easy to identify your neighbors.



The UINavigation control method setViewControllers is an option.

Another would be to release the earliest view controller with popViewControllerAnimated: In some cases popToRootViewControllerAnimated: would be better or even popToViewController: animate :. Hover, I personally was not able to use popToViewController: animated: but that might have been my fault at the time.

Yes, I think you need a root view controller. I myself tried to swap the root view controller the other day but couldn't do it. In the end it was probably not the most elegant solution, but I found it easier to implement some kind of dummy root view controller that doesn't show but displays my app logo in the background (same as the default image but moves in negative coordinates to match the default startup image. It lays behind the navigation bar and status bar.). It might show some kind of blank black background or so. After all, it will most likely never be seen.



All Articles