pdrcabrod
pdrcabrod

Reputation: 1477

Add a new viewcontroller to a tab controller

I have a tab bar controller created by a MainWindow.xib. I have 4 view controllers in it. Now i want to add a 5th item programatically (because I dont know which class I will have to use until compile time)

This is my code:

UIViewController * login = [[LoginUserViewController alloc] initWithNibName:@"LoginUserViewController" bundle:nil];

NSMutableArray * viewControllersArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[viewControllersArray addObject:login];
[self.tabBarController setViewControllers:viewControllersArray
                                 animated:YES];

But I get

[LoginUserViewController viewControllers]: unrecognized selector sent to instance 0x95791b0'

When I reach this code

UINavigationController *navController = [tabBarController.viewControllers lastObject];
LoginViewController * log = [navController.viewControllers objectAtIndex:0];

Where am I going wrong? Any ideas?

Thanks a lot

Upvotes: 0

Views: 253

Answers (3)

Rahul Wakade
Rahul Wakade

Reputation: 4805

You forgot that last tab is LoginUserViewController and not an instance of UINavigationController.

After adding LoginUserViewController to tab bar controller, your last view controller in tab bar controller's array will be LoginUserViewController and not an instance of UINavigationController

UINavigationController *navController = [tabBarController.viewControllers lastObject];

Hence above line will return LoginUserViewController's object in navController variable.

RecordsViewController *recordsViewController = [navController.viewControllers objectAtIndex:0];

Hence above line will cause crash as LoginUserViewController don't have viewControllers property.

Upvotes: 1

Khaled Barazi
Khaled Barazi

Reputation: 8741

If this is all your code, it does not look like you are instantiating the navigation controller. Look at:

initWithRootViewController:

in the UINavigatorClass. I would replace:

UINavigationController *navController = [tabBarController.viewControllers lastObject];

with:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: [tabBarController.viewControllers lastObject]];

Edit: One more thoughts: Looks like you lucked out on the "tabBarController" property since you might have synthesized it as @synthesize tabBarController=tabBarController;
I strongly recommend you use the latest version of XCode which will automatically do the @synthesize for you. The line before last in your code should be self.tabBarController

Upvotes: 1

VSN
VSN

Reputation: 2361

Try this....

  - (void) setUpTabBar {
FirstViewController *firstViewController = [[FirstViewController alloc]init];
firstViewController.title = @"First View";
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];

SecondViewController *secondViewController = [[SecondViewController alloc]init];
secondViewController.title = @"Second View";
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];

ThirdViewController *thirdViewController = [[ThirdViewController alloc]init];
thirdViewController.title = @"Third View";
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

ForthViewController *forthViewController = [[ForthViewController alloc]init];
forthViewController.title = @"Forth View";
forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController];

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil];
tabBarController.delegate = self;             
[self sizeViewToAvailableWindow:[tabBarController view]];

[firstNavController release];
[firstViewController release];

[secondNavController release];
[secondViewController release];

[thirdNavController release];
[thirdViewController release];

[forthNavController release];
[forthViewController release];
}

Upvotes: 0

Related Questions