how to set individual tabbaritem icons in uitabbarcontroller in cocoa - cocoa-touch

I was answered how to set images in general for a uitabbarcontroller. however my uitabbarcontroller is an array of views that looks like:
tabBarController = [[UITabBarController alloc] init];
viewTab1controller = [[ViewTab1Controller alloc] initWithNibName:#"ViewTab1" bundle:nil];
viewTab1controller.title = #"Schedules";
navigationTab1Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab1controller] autorelease];
[viewTab1controller release];
viewTab2controller = [[ViewTab2Controller alloc] initWithNibName:#"ViewTab2" bundle:nil];
viewTab2controller.title = #"Nearest Stop";
navigationTab2Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab2controller] autorelease];
[viewTab2controller release];
viewTab3controller = [[ViewTab3Controller alloc] initWithNibName:#"ViewTab3" bundle:nil];
viewTab3controller.title = #"Routes";
navigationTab3Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab3controller] autorelease];
[viewTab3controller release];
viewTab4controller = [[ViewTab4Controller alloc] initWithNibName:#"ViewTab4" bundle:nil];
viewTab4controller.title = #"Feedback";
navigationTab4Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab4controller] autorelease];
[viewTab4controller release];
//viewTab5controller = [[ViewTab5Controller alloc] initWithNibName:#"ViewTab5" bundle:nil];
//navigationTab5Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab5controller] autorelease];
//[viewTab5controller release];
tabBarController.viewControllers = [NSArray arrayWithObjects:
navigationTab1Controller,
navigationTab2Controller,
navigationTab3Controller,
navigationTab4Controller,
//navigationTab5Controller,
I was given the code in the previous answer to add an image to a tabbaritem:
viewController.tabBarItem.image = [UIImage imageNamed:#"foo.png"];
However this doesn't specify the specific tabbbaritem.
How do I assign an image to each of these 4 tabs?
Thanks!
nil];

Do it like this for each View Controller that you'll be adding to your Tab Bar:
viewTab1controller = [[ViewTab1Controller alloc] initWithNibName:#"ViewTab1" bundle:nil];
viewTab1controller.title = #"Schedules";
navigationTab1Controller = [[UINavigationController alloc] initWithRootViewController:viewTab1controller];
navigationTab1Controller.tabBarItem.image = [UIImage imageNamed:#"Match.png"];

Related

How to add separator between tabbar icon

How to add a separator line between each icon for TabBar.
//TabbarViewController.h
#import <UIKit/UIKit.h>
#interface TabBarViewController : UITabBarController <UITabBarControllerDelegate>{ }
#end
//TabbarViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
HomeViewController *HomeViewController = [[HomeViewController alloc] init];
UploadViewController *UploadViewController = [[UploadViewController alloc] init];
MapViewController *mapviewController = [[MapViewController alloc]init];
FavouriteViewController *favouriteViewController = [[FavouriteViewController alloc]init];
HomeViewController.title = #"Home";
UploadViewController.title = #"Upload";
mapviewController.title = #"Map";
favouriteViewController.title = #"Favourite";
UINavigationController *homeNavCont = [[UINavigationController alloc] initWithRootViewController:HomeViewController];
UINavigationController *secondNavCont = [[UINavigationController alloc] initWithRootViewController:UploadViewController];
UINavigationController *thirdNavCont = [[UINavigationController alloc] initWithRootViewController:mapviewController];
UINavigationController *fourthNavCont = [[UINavigationController alloc] initWithRootViewController:favouriteViewController];
[[homeNavCont tabBarItem] setImage:[UIImage imageNamed:#“Home32”]];
[[secondNavCont tabBarItem] setImage:[UIImage imageNamed:#"camera32"]];
[[thirdNavCont tabBarItem] setImage:[UIImage imageNamed:#"Navigation32"]];
[[fourthNavCont tabBarItem] setImage:[UIImage imageNamed:#"Faviourte32"]];
HomeViewController.tabBarItem.selectedImage = [[UIImage imageNamed:#"Home32"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
UploadViewController.tabBarItem.selectedImage = [[UIImage imageNamed:#"camera32"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
mapviewController.tabBarItem.selectedImage = [[UIImage imageNamed:#"Navigation32"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
favouriteViewController.tabBarItem.selectedImage = [[UIImage imageNamed:#"Faviourte32"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
self.viewControllers=[NSArray arrayWithObjects:homeNavCont,secondNavCont,thirdNavCont,fourthNavCont,nil];
self.delegate = self;
[self setSelectedIndex:0];
}
Need your advice for adding separator in between icons for tabbar.

Set view controller as the first controller in UITabBarController

I have this code in my appDelegate.m that implements a UINavigationController with a UITabBarController:
FristViewController *primeiro = [[FristViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] init];
[nav1 pushViewController:primeiro animated:YES];
FilesViewController *segundo = [[FilesViewController alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] init];
[nav2 pushViewController:segundo animated:YES];
InfoViewController *terceiro = [[InfoViewController alloc] init];
UINavigationController *nav3 = [[UINavigationController alloc] init];
[nav3 pushViewController:terceiro animated:YES];
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.viewControllers = [NSArray arrayWithObjects:nav2, nav1, nav3, nil];
nav1.tabBarItem.image = [UIImage imageNamed:#"tab1.png"];
nav2.tabBarItem.image = [UIImage imageNamed:#"tab2.png"];
nav3.tabBarItem.image = [UIImage imageNamed:#"tab3.png"];
self.window.rootViewController = tab bar;
It's all right with this code, in my case when I beginning my app appearing on the first controller is FilesViewController because it is the first in the order of Tab Bar, but in my case I would like the first controller was the FristViewController without changing the order of items in the tab bar, how can I do this?
Go to your storyboard or xib where you created your TabBar. Drag the First controller to 1st position. Or programmatically you could set tabBar.selectedIndex = 0;

UITabBarItem don't show any icon images?

I have this code:
MeuPrimeiroViewController *primeiro = [[MeuPrimeiroViewController alloc] init];
MeuSegundoViewController *segundo = [[MeuSegundoViewController alloc]init];
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.viewControllers = [NSArray arrayWithObjects:primeiro,segundo, nil];
primeiro.tabBarItem.title = #"Primeiro";
primeiro.tabBarItem.image = [UIImage imageNamed:#"1.jpg"];
segundo.tabBarItem.title = #"Segundo";
segundo.tabBarItem.image = [UIImage imageNamed:#"3.jpg"];
self.window.rootViewController = tabbar;
I don't know why, the images don't show?
You need to use the tabBarItem initWithTitle:image:selectedImage method instead. See the method definition here.

App crashes on start

Hi I was doing some testing earlier and my app was running just fine. I wanted to do some more testing so I decided to remove the app from my device and then reinstall it by running.
Well now for some reason I get to the stage where my splash screen shows up and then it crashes and I get the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
This obviously means theres an array out of bounds correct? But why now and how can I found out where and what view controller this is happening on? How could it run before and now all of a sudden when I try reinstalling the app through running it again I get this error?
Thanks
EDIT
The error is with the array in the following code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
exploreViewController *view1 = [[exploreViewController alloc] initWithNibName:#"exploreViewController" bundle:nil];
view1.title= #"Explore";
Upcoming *view2 = [[Upcoming alloc] initWithNibName:#"Upcoming" bundle:nil];
view2.title = #"Upcoming";
calcViewController *view3 = [[calcViewController alloc] initWithNibName:#"calcViewController" bundle:nil];
view3.title = #"Calc";
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:view2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:view3];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nil];
self.tabBarItem = [[[UITabBarItem alloc] init] autorelease];
NSArray *tabBarItems = self.tabBarController.tabBar.items;
UIImage *tab1 = [UIImage imageNamed:#"85-trophy.png"];
UIImage *tab2 = [UIImage imageNamed:#"12-eye.png"];
UIImage *tab3 = [UIImage imageNamed:#"237-key.png"];
NSArray *tabBarImages = [[[NSArray alloc] initWithObjects:tab1, tab2, tab3,nil]autorelease];
NSInteger tabBarItemCounter;
for (tabBarItemCounter = 0; tabBarItemCounter < [tabBarItems count]; tabBarItemCounter++)
{
tabBarItem = [tabBarItems objectAtIndex:tabBarItemCounter];
tabBarItem.image = [tabBarImages objectAtIndex:tabBarItemCounter];
}
Well, the reason for this crash is the following:
You are adding five items to your tabBar (nav1, nav2, nav3, nav4, nav6), but you only have three images for your tabs (tab1, tab2, tab3). So when the for loop reaches the fourth tab it crashes because tabBarImages only contains three objects.
Apart from that your code looks a bit messy - which could be the reason for not seeing the error on first sight.
// edit
You are complicating things - just try the following piece of code
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav1.title = #"Explore";
nav1.tabBarItem.image = [UIImage imageNamed:#"85-trophy.png"];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav2.title = #"Upcoming";
nav2.tabBarItem.image = [UIImage imageNamed:#"12-eye.png"];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav3.title = #"Calc";
nav3.tabBarItem.image = [UIImage imageNamed:#"237-key.png"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:nav1, nav2, nav3, nil]];
[nav1 release];
[nav2 release];
[nav3 release];

I can not set title to my programatically tab bar controller

I try to create tab bar controller as programatically. It is ok but I can not set title to tab bar items.. How can I do this?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
tab1 = [[ZiyaretFormTab1 alloc] initWithNibName:#"ZiyaretFormTab1" bundle:nil];
tab2 = [[ZiyaretFormTab2 alloc] initWithNibName:#"ZiyaretFormTab2" bundle:nil];
tab3 = [[ZiyaretFormTab3 alloc] initWithNibName:#"ZiyaretFormTab3" bundle:nil];
tab4 = [[ZiyaretFormTab4 alloc] initWithNibName:#"ZiyaretFormTab4" bundle:nil];
tab5 = [[ZiyaretFormTab5 alloc] initWithNibName:#"ZiyaretFormTab5" bundle:nil];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:#"title" image:nil tag:0];
tab1.tabBarItem = item;
myTabBarController.viewControllers = [NSArray arrayWithObjects: tab1, tab2,tab3,tab4,tab5,nil];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
1 in this line means, that you try to get second object from array.
Also UITabBarItem has nice method initWithTitle:image:tag:. And this is link to documenttion its very helpfull.
My solve:
1) Create some items with:
initWithTitle:image:tag:
2) Add them to your tabbar with tab bar method:
- (void)setItems:(NSArray *)items animated:(BOOL)animated