How to switch between existing views?

Discussion in 'iOS Programming' started by cthesky, Oct 13, 2012.

  1. cthesky macrumors member

    Aug 21, 2011
    Hi all,

    I have a Root View Controller and 3 other view controllers (First View Controller, Second View Controller, Third View Controller). I added First View Controller as a subview of Root View Controller, second View Controller as a subview of First View Controller, Third View Controller as a subview of Second View Controller by using addSubview method. I can switch from Third View Controller to Second View Controller by using [self.view removeFromSuperview]. But how to switch from Third View Controller to First View Controller? Is it add the First View Controller as a subview of Third View Controller? Am I doing something wrong?

    Any comments and suggestions are welcome. Thanks a lot. :)
  2. CodeBreaker macrumors 6502

    Nov 5, 2010
    Sea of Tranquility
    Hmm, a no brainer solution wil be to add all three view controllers' views to the root view controller's view. Then to show/hide a particular view controller, you would do:

    // Shows the 2nd view controllers view. Hides others.
    [self.firstViewController.view setHidden:YES];
    [self.secondViewController.view setHidden:NO];
    [self.thirdViewController.view setHidden:YES];
    But this is generally a bad idea. This blog post explains why.
  3. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia

    Gak. My suggestion is to not do that.

    You should look at using a navigation controller. Navigation controllers are designed to manage a stack of view controllers, and let you push one after another onto the top of the stack. You then pop off view controllers when you are done with them.

    You should not put a view controller's content view inside another view controller unless you're using the parent/child view controller support that was added in iOS 5.

    Unless you're using parent/child view controllers, or one of Apple's "off the shelf" container view controllers like a navigation controller, split view controller, or various others, a view controller is intended to manage the entire screen. Anything else is going to cause you lots of problems, and may even get you rejected from the app store.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Where did you learn this technique? Have you read the entire View Controller Programming Guide yet?

Share This Page