如何以编程方式将导航栏添加到我的应用程序?

问题描述:

我前段时间使用教程在界面生成器中设置导航栏来设置我的应用程序,但不再在我的任何应用程序中使用界面生成器,并且很想改变这一点,它使用界面生成器来编码所以我的问题是,我有一个导航栏工作,并出现在我的应用程序,HomeView的第一个视图。我会如何通过编程方式来做到这一点?如何以编程方式将导航栏添加到我的应用程序?

在AppDelegate.m文件,补充一点:

- (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]; 

} 

一定要在文件的顶部添加#import "RootViewController.h"

+0

有没有办法来补充一点,导航栏,而无需使用一个UINavigationController? – lowerkey 2011-08-09 00:12:21

程序添加导航栏的另一种方式,改变你的应用程序代理类似的application:didFinishLaunchingWithOptions方法:

- (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]; 

}