Speckpgh
Speckpgh

Reputation: 3372

Using a View Controller for a subView

Background:

I have an application that has a tab bar as part of its interface, however I am not using a tab bar controller... I did this on purpose for many reasons so please don't answer me with rewrite the app as a tab bar controller, because that's not going to help.

The view controller controls the screen and tabs etc quite well, but now I want to add a completely different view to the application. Now, yes, I could jsut put the view in the existing .XIB and then put all its controlls in the existing view controller class, but it already has a lot going on, so I would like to tie this view to its own view controller and launch, effectively on a button press.

Now, in a tab bar controller model, add a new view controller tied to a tab no issue, but since I am not using a tab bar controller, I am a bit lost. I know this is basic, and I'm just not seeing it, but in the XIB there is no way to add a viewcontroller to the tabbar or tabbar item, sicne its not a tabbarcontroller IB doesn't seem to allow this. I could easily add the view to IB, but then I don't have the controller tied to it and would need all the control code for that view to be part of the main controller as well, which I can do but would prefer to segregate, since this view is completely different from all other functions related to other user interactions and views.

So, what is the best way to accomplish this? Instantiate a the ViewController on the tab bar press, and add its view as a subview to the main window? I assume this will work but this seems a bit kludgy.. Is there some other way to show a view and tie it to a veiw controller?

Upvotes: 1

Views: 4002

Answers (2)

Rob
Rob

Reputation: 437532

If you use iOS 5's view controller containment, in viewDidLoad you would install the first view controller. I generally define a childView view in my parent controller's NIB/storyboard which will dictate where the subview is placed (this is esp useful if you want to animate transitions and want to limit the animation to the subview):

OneViewController *controller = [[OneViewController  alloc] initWithNibName:@"OneViewController"  bundle:nil];

[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
[childView addSubview:controller.view];

Then, when you click on, for example, your second tab, you want to go to the next controller, you just invoke transitionFromViewController:

[self addChildViewController:newController];

[self transitionFromViewController:oldController 
                  toViewController:newController
                          duration:0.5
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            // do any other animation of the parent view that you want coordinated with the transition, e.g., I'm going to replace the nav bar title
                            [self updateTitles:newController];
                        }
                        completion:^(BOOL finished){
                            [oldController willMoveToParentViewController:nil];
                            [oldController removeFromParentViewController];
                            [newController didMoveToParentViewController:self];
                        }];

Clearly, doing all of this, you're doing a lot of work to reproduce the standard tab bar controller functionality, but I presume you have compelling reasons to do so.

Upvotes: 3

Husker Jeff
Husker Jeff

Reputation: 857

What you propose in your last paragraph will work...mostly. :-) View Controllers are really meant to manage "a screenful of content," so if you try this, be prepared for quirks with unloading or reloading views, autorotation and other subtleties. You might be wise to follow this advice in Apple's view controller programming guide:

Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.

Upvotes: 0

Related Questions