Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cthesky

macrumors member
Original poster
Aug 21, 2011
91
0
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. :)
 

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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. :)

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:

Code:
// 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.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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. :)


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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.