Nils Brenkman
Nils Brenkman

Reputation: 71

How do I switch ViewController instead of adding to stack?

I have a number of objects linking to each other in a circle. Each object holds a reference to it's neighbors, square and triangle are different class types:

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

The tow classes, triangle and square, are made visible with a ViewController, and they link to each other with segues.

So far, no problem. However, while I'm browsing around in my structure, I keep adding ViewControllers on top of each other. Not only does this seem a bad practice memory-wise, but it also presents the problem that when I want to quit this structure, I have to back-track by closing all ViewControllers that I opened.

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

I've been looking for a while for a solution, but have little success. So I feel that doing what I want is either impossible, or I'm just not getting an obvious point and don't know what to look for. Do I need a RootViewController for something like this? Or should I create a custom segue that dismisses the old ViewController before adding the new one? I'm really at a loss here.

Upvotes: 3

Views: 2003

Answers (5)

Hermann Klecker
Hermann Klecker

Reputation: 14068

The UINavigation controler's method setViewControllers is an option.

Another would be to pop the most rencent view controller using popViewControllerAnimated: In some cases popToRootViewControllerAnimated: would be best or even popToViewController:animated:. Hoever, I personally was not successful using popToViewController:animated: but that may have been my fault at that time.

Yes, I think you need a root view controller. I myself tried to exchange the root view controller the other day but failed doing so. In the end it was probably not the most elegant solution but easier for me to implement some dummy root view controller which does nothting but display my app logo in the background (Same as the default image but moved into negative coordinates in order to match the default image on startup. It is laying 'behind' the navigation bar and status bar.). It could show some empty black background or so. In the end it is will most probably never be visible.

Upvotes: 1

DBD
DBD

Reputation: 23233

Sounds like you want to do one of a couple things

  1. Replace your rootViewController
  2. Have a rootViewController which acts as a container for a single UIViewController and change that out as needed. In iOS 5 you can do this with a custom UIViewController, but or you could use one which Apple provided.UINavigationController` can do it, but unless you are also using it to navigate a tree like structure of view controllers it's probably not the best option.

Your best answer sort of relies on your need.

If your controllers don't have much state or don't get swapped in and out a lot you could use option 1.

If you expect users to swap between controllers often and quickly and/or your controllers require significant setup or have a lot of state then you might want to a NSArray and just use presentViewController:animated:completion: to show different controllers when needed. Storing your controllers in a NSArray has the added bonus of easily being able to identify their neighbors.

Upvotes: 1

dasdom
dasdom

Reputation: 14073

You should have a look at the UINavigationController method setViewControllers:animated:.

Upvotes: 1

Muhammad Saqib
Muhammad Saqib

Reputation: 991

Add all subView once , in viewDidLoad and give tags to all you SubView after that where you you want to show that view in viewController don't add it just bring it to front by calling the function [[self.view viewWithTag:1]bringToFront]

Upvotes: 2

calimarkus
calimarkus

Reputation: 9977

Since you are probably using a navigationcontroller, you should have a look at the UINavigationController reference. There are methods to modify the navigation stack. You cannot do this only with a storyboard. You will need some custom code.

Upvotes: 1

Related Questions