I\'m developing an app for iOS5 and up and I don\'t use storyboards or IB. I\'m creating a custom UITabBarController
and in my AppDelegate
I\'m put
Should I create a separate UINavigationController for each tab
If you want to navigate in each tab, YES you should add each viewController embedded in a navigationController.
Suppose you have a tabbarController. Now you can add any viewController or any NavController in your tabController. NavController can contain viewController. But you might have confusion where you will use navController or viewController. You will use viewController where you don't need navigation, I mean where you don't need.
Here is an code example where the first view contains only view and the second view contains navigation controller. You can't push new view in first view, but you can easily push new view in second view.
-(void)addTabBarControllers
{
UIViewController *viewController1, *viewController2;
viewController1 = [[[HomeView alloc] initWithNibName:@"HomeView" bundle:nil] autorelease];
viewController2 = [[[FloorPlanHome alloc] initWithNibName:@"FloorPlanHome" bundle:nil] autorelease];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, nav2, nil];
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"First View"];
[[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Second View"];
[[self.tabBarController.tabBar.items objectAtIndex:0] setImage:[UIImage imageNamed:@"first.png"]];
[[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:@"second.png"]];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
Call this method from didFinishLaunchingWithOptions in AppDelegate. here HomeView and FloorPlanView is two different views, you need to add these views and class file first.
Refer to my answer here:
UITabBarController Issue
if(!self.tabBarController)
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
NSMutableArray *localcontrollerarray = [[NSMutableArray alloc] initWithCapacity:2];
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[localcontrollerarray addObject:navi1];
UIViewController *viewController2 = [[ScanViewController alloc] initWithNibName:@"ScanViewController" bundle:nil];
UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[localcontrollerarray addObject:navi2];
self.tabBarController.viewControllers = localcontrollerarray;
[self.window addSubview:self.tabBarController.view];
Yes, you can. Try something like this code in yourUITabBarController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray* sectionViewControllers = nil;
NSArray* controllers = [self controllers];
UIViewController* controller = nil;
for (controller in controllers)
{
if (sectionViewControllers == nil)
sectionViewControllers = [NSMutableArray arrayWithCapacity:0];
UINavigationController* navigationController = [[UINavigationController allocWithZone:[self zone]] initWithRootViewController:controller];
navigationController.navigationBarHidden = YES;
[sectionViewControllers addObject:navigationController];
[navigationController release];
}
self.viewControllers = sectionViewControllers;
}
- (NSArray*)controllers
{
if (!_controllers)
_controllers = [NSArray arrayWithObjects:[self tabController1], [self tabController2], nil];
return _controllers;
}
and this in you AppDelegate.m:
self.window.rootViewController = self.yourUITabBarController;