There’s code below, but this is also a conceptual issue. The question: Are the methods below the right way to move through view controllers? And that’s broad, so let me add some specific details.
Say you’re in View A. This view presents options to go to View B or View C. After the user makes the choice (i) I want to load the new view and (ii) the user won’t be going back to View A. (Also, btw, View A was my initial rootViewController.)
So, say I wire up two buttons in View A, each to load its respective view. Is this an efficient/solid way to do it:
- (IBAction)loadViewB:(id)sender
{
ViewBController *viewBController = [[ViewBController alloc] initWithNibName:Nil bundle:Nil];
[self.view.superview insertSubview:viewBController.view atIndex:0];
[self.view removeFromSuperview];
}
- (IBAction)loadViewC:(id)sender
{
ViewCController *viewCController = [[ViewCController alloc] initWithNibName:Nil bundle:Nil];
[self.view.superview insertSubview:viewCController.view atIndex:0];
[self.view removeFromSuperview];
}
The “insertSubview:atIndex:” method seems to be right, based on Apple’s View Controller Programming Guide. Also, the code works. But should I call the “removeFromSuperview”? Or should I just stack them up? (And if anyone has comments about memory management, I’m all ears.)
Also:
- I don’t want to move through structured data. So, it doesn’t seem like using a UINavigationController would be right.
- I don’t want to temporarily interrupt flow. So, strike a modal view.
I know it’s a simple question, but I just want to make sure I’ve understood the documentation right and have grasped the concept generally.
Thanks ahead of time.
I don’t think there’s anything wrong with your approach. Just beware that simply
[self.view removeFromSuperview]might not cause any memory to be freed up, for example if your app delegate holds a reference to your root view controller. It might be a good idea to give this view-switching responsibility to the app delegate, so it can choose to release the reference to the root view controller if it desires.