我想在基于视图的应用程序中添加导航控制器。我们如何以编程方式和使用xib文件来实现这一点..
答案 0 :(得分:2)
如果您需要在uiviewcontroller中加入导航控制器,则需要按照以下步骤进行初始化
UIViewController *yourViewController = ...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self presentModalViewController:navController animated:YES];
//you need to release the controller
[navController release];
如果你在UIApplicationDelegate方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
你不能做一个presentModalViewController:navController动画...然后你需要将navController.view添加到窗口
UIViewController *yourViewController = ...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self.window addSubview:navController.view];
//don't do a release of navController because is not retained by addSubview
答案 1 :(得分:0)
UINavigationController *navcontroller = [[UINavigationController alloc] initWithRootViewController:viewController];//here viewController is to which you want to make the navigation
[self.view addSubView:navController.view];
答案 2 :(得分:0)
您只需从Interface Builder右下角的对象中拖出“导航栏”即可。这基本上是Sachin在他的回答中所说的,但你仍然必须以编程方式创建导航控制器的功能。即将新视图推送到堆栈并将其弹出。
在我看来,最容易在代码中做到这一点。
答案 3 :(得分:0)
如果您想将导航控制器作为主窗口的根视图。然后,您可以使用以下代码执行此操作。
@interface yourAppDelegate_iPad : NSObject <UIApplicationDelegate> {
UINavigationController *navigationController;
}
@property (nonatomic, retain) UINavigationController *navigationController;
@end
@implementation yourAppDelegate
@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
您可以使用xib执行此操作,如下所示
- 打开
MainWindow.xib
- 将
UINavigationController
拖放到其中。- 创建并连接插座。
- 打开导航控制器的属性并设置根目录 图。
醇>