以编程方式实现导航控制器

时间:2012-04-24 02:17:24

标签: objective-c ios uinavigationcontroller

我以下列方式创建了6个视图控制器:

Truck_Tracker_AppAppDelegate *delegate = (Truck_Tracker_AppAppDelegate *)UIApplication.sharedApplication.delegate;
UIViewController *viewController1 = [[TrucksViewController alloc] initWithNibName:@"TrucksView" bundle:nil];
UIViewController *viewController2 = [[MapViewController alloc] initWithNibName:@"MapView" bundle:nil];
UIViewController *viewController3 = [[BlockPartyViewController alloc] initWithNibName:@"BlockPartyView" bundle:nil];
UIViewController *viewController4 = [[FavoritesViewController alloc] initWithNibName:@"FavoritesView" bundle:nil];
UIViewController *viewController5 = [[UserSettingsViewController alloc] initWithNibName:@"UserSettingsView" bundle:nil];
UIViewController *viewController6 = [[TOSettingsViewController alloc] initWithNibName:@"TOSettingsView" bundle:nil];

我需要以编程方式将viewController1,3和4包装在导航控制器中。我在Apple Developer的网站上找到了这个:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *myViewController = [[MyViewController alloc] init];
navigationController = [[UINavigationController alloc]
                            initWithRootViewController:myViewController];

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}

但我需要在创建viewControllers的地方执行此操作。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

嗯,首先要了解一些关于UINavigationController的事情。通过使用UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:myViewController];,您实际上是为类myViewController创建导航控制器,它自动为其授予默认的UIToolBar和默认的UINavigationBar。因为UINavigationController是UIViewController的子类,所以它可以在任何以所述类作为参数的函数中自由使用。

我假设你想要在UITabbar中使用它,所以为每个视图控制器创建一个单独的UINavigationController实例,将它们收集到NSArray *中,并将它们设置为等同于UITabbar上的'viewControllers'属性。

答案 1 :(得分:-1)

创建一个NSArray,其中包含指向视图控制器的指针,并将NSArray分配给导航控制器的viewControllers属性。