The situations described in this article are:
1. When creating a project, select use storyboards.
2. When creating a project, user storyboards is not selected and you want to add a storyboard later.
This article is a supplement to the previous article, which is not comprehensive ("do not mix the storyboard and code with uinavigationcontroller ").
The previous article deals with the first case. At this time, your function in appdelegate is very simple, as shown below ():
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // Override point for customization after application launch. TDRViewController *tdrVC=[[TDRViewController alloc] init]; [self.window addSubview:tdrVC.view];// self.navController=[[UINavigationController alloc] initWithRootViewController:tdrVC];// [self.window addSubview:self.navController.view]; return YES;}
The compiler associates mainstoryboard with appdelegate when creating the project, so you do not need to initialize self. window, the compiler will call the window in mainstoryboard. At this time, if you initialize a window, there will be problems (see the previous article ).
In the second case, your storyboard is added in the middle (named storyboard), so the storyboard is not associated with appdelegate, And the compiler will not automatically call the window in the storyboard. At this time, you need to initialize the window by yourself, and use the code to call the storyboard and set the rootviewcontroller.
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {// override point for customization after application launch. self. window = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds]; self. window. backgroundcolor = [uicolor whitecolor]; uistoryboard * storyboard = [uistoryboard storyboardwithname: @ "storyboard" Bundle: Nil]; // calls storyboard self. window. rootviewcontroller = [storyboard instantiateinitialviewcontroller]; // use the rootvc of the storyboard as the rootvc of the window [self. window makekeyandvisible]; return yes ;}
What if I want to use multiple storyboards in the project?
Add the secondstoryboard to link the viewcontroller you want on the page.
When you need to jump to the secondstoryboard, like this:
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil]; [self presentModalViewController:[secondStoryboard instantiateInitialViewController] animated:YES];
Supplement:
1. sometimes we find that the title set in viewcontroller in storyboard is not displayed when the button is running. The reason is that you use the code similar to the following: viewcontroller * Vc = [[viewcontroller alloc] init] generates a new viewcontroller instance, which is not associated with viewcontroller in storyboard.
To avoid this situation, you can use storyboard to generate viewcontroller instances. First, set storyboardid for viewcontroller in identity inspector and xcode4.6 as follows:
In earlier versions of xcode, the settings are in attributes Inspector-> View Controller-> identifier.
Then, use identifier to obtain the specified view controller from the storyboard:
UIStoryboard *mainStory=[UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil]; diskMainViewController *diskMainVC=[mainStory instantiateViewControllerWithIdentifier:@"diskMainVC"]; [self.navigationController pushViewController:diskMainVC animated:YES];