views:

521

answers:

2

I want to load a nib that isn't a view in a UITabViewController. Here's what I have now and it isn't working, but it should give you an idea of what I want:

- (IBAction)PlaylistButtonPressed:(id)sender
{
    MusicPick *music = [[MusicPick alloc] initWithNibName:@"MusicPick" bundle:nil];

    [self.view addSubview:music.view];

    [music release]; 
}

When you press a button in the view, the idea is to load another nib, MusicPick, that will load as a subview, pick something and come right back to here. Any help is appreciated, or new ideas.

+3  A: 

You cannot add a subview directly to a UITabBarController. The way one of these controllers works is by storing a list of UIViewControllers and displaying each of these in a tab. But they aren't really subviews, per sé. You don't really modify the tab bar controller itself ever except to update this list.

Since what you seem to want to do is present a temporary view to allow the user to select some options that will affect something in the tab bar controller, I'd suggest you present this view from your nib as a modal view over the tabbed views. Take a look at UIViewController's presentModalViewController:animated: and dismissModalViewControllerAnimated: methods. Assuming MusicPick is a subclass of UIViewController, just pass it to this method after it has been allocated (such as you did it in your first line of code above), and UIKit will take care of the rest. Remember to release that MusicPick instance when you're done pulling the selected values or user-entered data from it.

Marc W
+1  A: 

To add a little more detail.

You can add a view to an existing UIViewController's view by using addSubView, or by pushing a controller on to the UITabBarController's view. In the latter case, the UITabBarController must be [have been] a UINavigationController with a RootViewController.

I suspect, this is what you mean. Therefore you would do something like the following.

- (IBAction)PlaylistButtonPressed:(id)sender
{
    // Load UIViewController from nib
    MusicPick *music = [[MusicPick alloc] initWithNibName:@"MusicPick" bundle:nil];

    // Add to UINavigationController's stack, i.e. the view for this UITabBarController view
    [self.navController pushViewController:music animated:YES];

    // Release music, no longer needed since it is retained by the navController
    [music release];   
}

This assumes you have a UINavigationController as a view in your UITabBarController and it's called navController.

If you just want to add a UIView to the UIViewController's view in the UITabBarController (e.g. overlay), then you can just use addSubView as you've already figured out, no UINavigation Controller necessary.

Jordan