R – How to load a NIB that isn’t a view in a UITabViewController

cocoa-touchiphonenibuitabbarcontroller

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.

Best Answer

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.