How do I add a navigationbar to my app programmatically?
I set up my app a while ago using a tutorial for setting up the navigationbar in interface builder, but no longer use interface builder in any of my app and would much like to cha开发者_运维问答nge this 1 thing which does use interface builder to being coded in. So my question is, I have a navigationbar which works, and which appears on the first view of my app, HomeView. How would I make this happen just as it does now, programmatically?
In the AppDelegate.m file, add this:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
}
Be sure to add #import "RootViewController.h"
at the top of the file.
Another way of adding the navigation bar programmatically, change the application:didFinishLaunchingWithOptions
method of your app delegate like:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
}
精彩评论