I have created 6 view controllers in the following way:
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];
I need viewController1, 3, and 4 to be wrapped in a navigation controller programmatically. I found this on Apple Developer’s website:
- (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];
}
But I need to do it where I’ve created the viewControllers. Any ideas?
Well, there’s some things you have to understand about UINavigationController first. By using
UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:myViewController];, you are actually creating the navigation controller for the class myViewController, which automatically grants it a default UIToolBar, and a default UINavigationBar. And because UINavigationController is a subclass of UIViewController, it can be freely used in it’s place in any function that takes said class as an argument.I assume you’ll be wanting to use this in a UITabbar, so create an individual UINavigationController instance for each view controller, gather them into an NSArray* and set them equal to the ‘viewControllers’ property on your UITabbar.