Showing a different XIB/NIB in an iOS app

半腔热情 提交于 2019-12-01 12:08:01

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6526416/showing-a-different-xib-nib-in-an-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!