Add new tab dynamically in IOS - ios

In Tab bar Controller, Initially I had added 3 tab bar items in it.After successful login, I need to add one more item dynamically in my current Tab bar Controller . Is it possible to add dynamically ? Below is the code i have tried but its not working.
if (AppContext.loginUser.userId!=nil) {
UITabBarItem *tabBarItem4;// = [tabBar.items objectAtIndex:3];
tabBarItem4.selectedImage = [[UIImage imageNamed:#"SelectedProfileTab"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:#"Profiletab"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = #"Profile";
tabBarItem4.tag =4;
ProfileVC *profile = loadViewController(TabbarSB, VC_Profile);
UINavigationController *nv=[[UINavigationController alloc] initWithRootViewController:profile];
NSMutableArray *arrayOfTabBars=[AppContext.mainTabbar.viewControllers mutableCopy];
[arrayOfTabBars addObject:nv];
// [AppContext.mainTabbar setViewControllers:nil];
[AppContext.mainTabbar setViewControllers:arrayOfTabBars];
//AppContext.mainTabbar.viewControllers = arrayOfTabBars;
// [self.tabBarController.tabBarController addChildViewController:profile];
}

You can do that programmatically:
if (AppContext.loginUser.userId!=nil) {
// First, create your view controller
ProfileVC *profile = loadViewController(TabbarSB, VC_Profile);
// then embed it to a navigation controller
// this is not required, only if you need it
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:profile];
// Get viewControllers array and add navigation controller
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[viewControllers addObject:nav];
// Set back the array
[self.tabBarController setViewControllers:viewControllers];
// Create tab bar item for ProfileVC
profile.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image selectedImage:selectedImage];
}

Related

Dynamically reload UIViewControllers on language change

I am changing UITabBarItem title after the user changes his language preferences in the app settings. The problem is that the whole item disappears for a while after this change is made and then appears again with the new title.
In AppDelegate I am initializing UITabBarController:
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
BFNCategoriesTableViewController *shopViewController = [[BFNCategoriesTableViewController alloc] init];
UINavigationController *shopNavigationController = [[UINavigationController alloc] initWithRootViewController:shopViewController];
shopNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:BFNLocalizedString(kTranslationShop) image:[UIImage imageNamed:#"TabBarShopUnselected"] selectedImage:[UIImage imageNamed:#"TabBarShopSelected"]];
self.tabBarController.viewControllers = #[offersNavigationController,
shopNavigationController,
//wishlistNavigationController,
cartNavigationController,
moreNavigationController];
Then after the translation is downloaded I just set different title like this:
navigationController.title = BFNLocalizedString(kTranslationCart);
Do you have an idea why this happens and eventually how to ovecome this issue?
Thanks.
Firstly, I can't understand regarding code above, how tabBarController is connected with UINavigationController.
You should
self.tabBarController.viewControllers = [NSArray arrayWithObject: shopViewController];
UINavigationController *shopNavigationController = [[UINavigationController alloc] initWithRootViewController: self.tabBarController ];
UINavigationController doesn't have a property is tabBarItem.
Following that:
shopViewController.title = = BFNLocalizedString(kTranslationCart);
It changes a title is on top of navController and title of tabBar as well.
Another way, It can change a tabBarTitle directly.
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem * itemBar = [ [UITabBarItem alloc] initWithTitle:BFNLocalizedString(kTranslationCart)
image:(UIImage *)image // your image
selectedImage:(UIImage *)selectedImage]; // highlighted image
[tabBar setItems:[NSArray arrayWithObject:itemBar
animated:YES ];
I think a first way is enough it must certainly work.

UITabBarController with unselected tab

I have a view controller say 'HomeViewController' on which I have added a UITabBarController, which has 4 UITabBarItems. Now what I want is, when I select the HomeViewController no tab should should be selected for the first time and another view should be loaded. I have tried UITabBar instead of UITabBarController but its not working. I have also tried :
tabBarController.selectedViewController=nil; //giving crash
tabBarController.selectedIndex=-1; //not working either
UITabBarItems should be selected when user will click on them.
You have to add UITabBar object on HomeViewController.
In ViewDidLoad you have to add following code:
ViewController1 *controller1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
ViewController2 *controller2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
viewControllersArray = [[NSArray alloc] initWithObjects:controller1,controller2, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [[NSArray alloc] initWithArray:viewControllersArray];
for(int i = 0; i < tabBarController.tabBar.items.count; i++){
[[tabBarController.tabBar.items objectAtIndex:i] setTitle:[[tabBar.items objectAtIndex:i] title]];
}
isItemSelected = NO;
Also set the tag of tab bar items starting from 0. As 0 for first item, 1 for second item and so on.
and implement delegate method:
(void)tabBar:(UITabBar *)tabBar1 didSelectItem:(UITabBarItem *)item{
if(!isItemSelected){
isItemSelected = YES;
AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDel.window.rootViewController = tabBarController;
}
tabBarController.selectedIndex = item.tag;
tabBarController.selectedViewController = [viewControllersArray objectAtIndex:item.tag];
}
I think this is not the right way, as Concept of creating TabBar says that " Show the particular controller of related tab". If u do not select any tab than what else you will show on screen, "A black screen ?". so please re-architecture your design.

UIView added programmatically to UITabBarController displays just black

I am programmatically changing the tab bar item for my tab bar at runtime.
The whole UI of my app uses Storyboards and this is the only area where I am making the UI changes in code.
So, the UI changes depending on a property. If this property cannot be found, the array of viewControllers changes to the new array that contains the new UIView.
Here's my code for the TabBarController.m:
NSArray *arrayOfControllers = self.viewControllers;
NSMutableArray *newArrayOfControllers = [[NSMutableArray alloc]init];
for (id controller in arrayOfControllers)
{
[newArrayOfControllers addObject:controller];
}
if ( !isMember)
{
NotMemberVC *vc = [[NotMemberVC alloc]init];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Test" image:[UIImage imageNamed:#"door-sign.png"] tag:0];
[newArrayOfControllers removeLastObject];
[newArrayOfControllers addObject:vc];
self.viewControllers = newArrayOfControllers;
}
And here's what it looks like when I select this new tab bar item:
Any suggestions as to how I can fix this?
First of all you don't need to do a for cycle when you have a method to do a mutableCopy of the array:
NSArray *arrayOfControllers = self.viewControllers;
NSMutableArray *newArrayOfControllers = [arrayOfControllers mutableCopy];
then here, you was not initializating the viewController with the view. In this case you have your VC with the UIView in your storyboard, and so you have to choose a StoryboardID in your VC in the Storyboard, and then:
if ( !isMember)
{
NotMemberVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Your_Identifier_ViewController_Storyboard"];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Test" image:[UIImage imageNamed:#"door-sign.png"] tag:0];
[newArrayOfControllers removeLastObject];
[newArrayOfControllers addObject:vc];
//Here make a copy to come back to immutable array
self.viewControllers = [newArrayOfControllers copy];
}

IOS Adding a TabBar Controller to a view

My IOS app has a login sequence that cannot be modified, once the sequence is complete I do the following in the app delegate
- (UIViewController*)newRootViewController {
NViewController *nView = [[NViewController alloc]
initWithNibName:#"NViewController"
bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:nView];
[nView release];
return navVC;
}
Once in nView is it possible to add a tab bar controller or how can I replace nView with a tab bar controller.
You can create a UITabBarController then add your NViewController and other controllers to the tab bar.
NViewController *nView = [[NViewController alloc]
initWithNibName:#"NViewController"
bundle:nil];
//Create my tab bar
UITabBarController* myTabController = [[UITabBarController alloc]init];
//Add my tabs
NSArray* tabs = [[NSArray alloc]initWithObjects:nView, nil];
[myTabController setViewControllers:tabs];

UITabBar view programmatically chosen on tap

I had a navigation controller based application. And I decided to use tab bars in my application.
When the user presses at a certain tab bar item I want to display a certain view controller - and I want programmatically in my code choose which one to display.
I tried to add in the Interface Builder a navigation controller into my tab bar, but viewWillAppear of its view controller is not being called.
How can I implement this feature?
I don't know if it's the "right way", but here's how I usually do this with three tabs.
- (void)initControls {
// Create the window.
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
// Create Tab Bar.
tabCon = [[UITabBarController alloc] init];
// Local array variable that holds the viewcontrollers.
// Capacity corresponds to the number of VC's
NSMutableArray *localVCArray = [[NSMutableArray alloc] initWithCapacity:3];
MyFirstViewController *oneViewController = [[MyFirstViewController alloc] init];
UINavigationController *oneNavCon = [[UINavigationController alloc] initWithRootViewController:oneViewController];
[localVCArray addObject:oneNavCon];
[oneViewController release];
[oneNavCon release];
MySecondViewController *twoViewController = [[MySecondViewController alloc] init];
UINavigationController *twoNavCon = [[UINavigationController alloc] initWithRootViewController:twoViewController];
[localVCArray addObject:twoNavCon];
[twoViewController release];
[twoNavCon release];
MyThirdViewController *threeViewController = [[MyThirdViewController alloc] init];
UINavigationController *threeNavCon = [[UINavigationController alloc] initWithRootViewController:threeViewController];
[localVCArray addObject:threeNavCon];
[threeViewController release];
[threeNavCon release];
// Set the tab bars array of view controllers to the localVCArray
[[self tabCon] setViewControllers:localVCArray animated:YES];
// Release the localVCArray, all of its contents are now retained by tabCon.
[localVCArray release];
// Add controls to window and show.
[window addSubview:[tabCon view]];
[window makeKeyAndVisible];
}
In the init method each viewController you can do something like:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
To set the icon used in the tab bar, the title in the tab bar, and the title of you navigation item.

Resources