Sam Spencer
Sam Spencer

Reputation: 8609

Showing a different XIB/NIB in an iOS app

I have multiple nib (xib) files and I want the user to see a different one when they tap a button. What I am looking for:

- (IBAction)buttonTap {
//Code for showing a different nib goes here
}

I can't figure out how to do this. I can show a different view within the nib file just fine, but I can't get it to show a different nib. How do I show a different nib when the user taps a button?

Any help is appreciated! Thanks!

Upvotes: 0

Views: 1370

Answers (1)

George Johnston
George Johnston

Reputation: 32258

The way I handle switching between actual xib's, and I'm sure there are a multitude of ways to accomplish the same thing, is to have my App Delegate act as a routing center between my views.

I subscribe my App Delegate to recieve events from button presses for existing views. When it recieves a request to switch views, such as a button press, I do something like this:

- (void) showLogin
{  
    LoginViewController *loginViewController = [[LoginViewController alloc]
                                                   initWithNibName:@"LoginViewController" bundle:nil];

    // Show
    self.loginViewController = loginViewController;
    [loginViewController release];

    self.window.rootViewController = self.loginViewController;
}

I set my rootViewController to the view I am attempting to display. It doesn't release the old controller, but simply replaces the view being displayed. You can place more logic in to determine if it's already displayed, close out other views, etc. In most simplistic terms, this works for me.

Upvotes: 2

Related Questions