programmatically create TABBAR - iphone

i created TAb bar programatically in this manner
UITabBarController *tabBarController = [[UITabBarController alloc] init];
contacts *vc1 = [[contacts alloc]init];
vc1.tabBarItem.image=[UIImage imageNamed:#"contacts.png"];
search* vc2 = [[search alloc] init];
vc2.tabBarItem.image=[UIImage imageNamed:#"search.png"];
a1* vc3 = [[a1 alloc] init];
a2 *vc4 = [[a2 alloc] init];
a3 *vc5 = [[a3 alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2,vc3,vc4,vc5, nil];
tabBarController.viewControllers = controllers;
[self.view addSubview:detailNavCont];
// Add the tab bar controller's current view as a subview of the window
[self.view addSubview:tabBarController.view];
What i want to accomplish is, i want to assign images to tab bar, firstly i tried on first 2 tabs, is showing a blue block instead of image.
secondly, when we create tab bar through Interface builder, there are custom tab bar item, like, contacts, search, bookmark ,compose etc.
so, if i want to assign contacts or search image to my tab bar items,which looks like the one in IB, how can i do it??
regards

Try something like this
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
HomeTabViewController *ptr_homeTab;
ptr_homeTab = [[HomeTabViewController alloc]initWithNibName:#"HomeTabViewController" bundle:nil];
UINavigationController *homeNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_homeTab];
homeNavBar.tabBarItem.title=#"Home";
homeNavBar.tabBarItem.image=[UIImage imageNamed:#"home.png"];
[ptr_homeTab release];
myHospitalviewController=[[MyHospitalViewController alloc]initWithNibName:#"MyHospitalViewController" bundle:nil];
UINavigationController *myHospitalNavBar=[[UINavigationController alloc]initWithRootViewController:myHospitalviewController];
myHospitalNavBar.title=#"My Hospital";
myHospitalNavBar.tabBarItem.image=[UIImage imageNamed:#"myhospital.png"];
[myHospitalviewController release];
viewController = [[TreatMentiViewController alloc]initWithNibName:#"TreatMentiViewController" bundle:nil];
UINavigationController *hospitalNavBar=[[UINavigationController alloc]initWithRootViewController:viewController];
hospitalNavBar.tabBarItem.title=#"Hospital";
hospitalNavBar.tabBarItem.image=[UIImage imageNamed:#"hospital.png"];
[viewController release];
PersonalMedicineViewController *ptr_PersonalMedicine = [[PersonalMedicineViewController alloc] initWithNibName:#"PersonalMedicineViewController" bundle:nil];
UINavigationController *managerNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_PersonalMedicine];
managerNavBar.tabBarItem.title=#"Manager";
managerNavBar.tabBarItem.image=[UIImage imageNamed:#"manager.png"];
[ptr_PersonalMedicine release];
[localViewControllersArray addObject:homeNavBar];
[localViewControllersArray addObject:hospitalNavBar];
[localViewControllersArray addObject:myHospitalNavBar];
[localViewControllersArray addObject:managerNavBar];
[homeNavBar release];
[hospitalNavBar release];
[myHospitalNavBar release];
[managerNavBar release];
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[localViewControllersArray release];
[window addSubview:tabBars.view];

Your images need to have a proper mask. You should save as png.

Related

Programmatically creating tab bar for ViewController

I've been looking at programatically adding a tab bar to my view controller because having a scroll view I can't place it on without it being in the middle of my view. I'm abit confused about how to add it. Does it need to be initiated in my ViewDidLoad method or my AppDelegate?
If I have:
UITabBarController *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release];
How can I allocate it to the bottom of my scrollview?
Thanks!
Now in my appDelegate class :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
ViewController* vc = [[ViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
tabBarController.viewControllers = controllers;
[_window addSubview:tabBarController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
It's crashing and not sure if it's a release I'm missing.
Edit:
For anyone else wondering this in Appdelegate.m:
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
Take a look at this Documentation given by Apple: ViewControllers.
Sample Code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UITabBarController *tabBarController = [[UITabBarController alloc] init];
FirstViewController* vc1 = [[FirstViewController alloc] init];
SecondViewController* vc2 = [[SecondViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
I did it this way. I copied this from my App Delegate and it works fine. Basically you add the view controllers to the tab bar then add the tab bar to the window's subview.
Instantiate the instance
iVacationTabBar = [[UITabBarController alloc] init];
However you create the views/view controllers:
UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools];
UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings];
UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about];
UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage];
UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup];
Add the controllers to the array:
NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage, rvc_tools,rvc_settings, rvc_backup, rvc_about, nil];
Set the array of view controllers to your tab bar:
[iVacationTabBar setViewControllers: controllers animated:NO];
Add the tab bar to the window's subview.
[_window addSubview:iVacationTabBar.view];
You can not add a UITabbarController on a UIViewController. Instead, on a ViewController you have to show the TabbarController, create a TabbarController, set ViewControllers for it, and add it to a Window.

Titles are not showing in the navigation bar iphone app

Titles are not showing after the home screen. I have put UITabBarController after the first screen. First screen hasn't got a UITabBarController. I am not sure the way I have implemented the tab.
I have set titles in every view controller by self.title = #"title". Titles are showing in tab items properly.
AppDelegate.m:
UIViewController *loginView= [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
UINavigationController *loginViewNavController = [[UINavigationController alloc] initWithRootViewController:loginView ];
self.navigationController = loginViewNavController;
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
LoginViewController.m:
-(IBAction)navigateMainMenu:(id)sender{
tabBarController = [[UITabBarController alloc] init];
MyAccountsViewController *vc1 = [[MyAccountsViewController alloc] initWithNibName:#"MyAccountsViewController" bundle:nil];
DepositsViewController *vc2 = [[DepositsViewController alloc] initWithNibName:#"DepositsViewController" bundle:nil];
MoreViewController *vc3 = [[MoreViewController alloc] initWithNibName:#"MoreViewController" bundle:nil];
PayTransViewController *vc4 = [[PayTransViewController alloc]init];
NSArray* controllers = [NSArray arrayWithObjects:vc1,vc4, vc2, vc3, nil];
tabBarController.viewControllers = controllers;
[self.navigationController pushViewController:tabBarController animated:YES];
[mainMenuViewController release];
If you set self.title = #"Title". This title will set in navController title. But In your TabBarController, every items are ViewController. Not an navigationController.
Issue: What you release in this line
[mainMenuViewController release];
mostly display title with bellow trickes :-
1. Self.title=#"Your Titile";
2. self.navigationItem.title=#"your Title";
3. when you using UItabBarController you can set Particular Viewcontroller as par your code Title like:-
vc1.title=#"your Title";
vc2.title=#"your Title";
vc3.title=#"your Title";
vc4.title=#"your Title";
I suggest you not you use tabbarcontroller inside a navigationcontroller. If you are using tabbarcontroller it should be the rootViewcontroller.

Tabbarcontroller with navigation controller

I am adding view controller in navigation controller and then adding it to tab bar controller. but if i add this like
navigationController.viewControllers =
[NSArray arrayWithObjects:rootViewController, rootViewController.photoViewController, nil];
tabBarController.viewControllers = [NSArray arrayWithObjects:tourNavigation,mapNavigation ,browserNavigation,
navigationController,nil];
This is not showing tab with fourth navigation controller.. other controllers are simple as
BrowserViewController *browserView = [[BrowserViewController alloc]initWithNibName:#"BrowserViewController"
bundle:nil];
browserView.title = #"Browser";
UINavigationController *browserNavigation = [[[UINavigationController alloc]initWithRootViewController:browserView]
autorelease];
This is working fine.. but navigation with array is not displaying.
I don't think you should be setting the navigation controller's viewControllers array like that. Instead try:
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navigationController pushViewController:rootViewController.photoViewController animated:NO];
This is assuming that you want the photo view controller to be showing when the user taps on the tab. It will be on top of the navigation controller's stack.
You need to start with view based application. And then create a UITabbarController in you appDelegate file.
Appdelegate.h
UITabBarController *tabBarController;
// set properties
Appdelegate.m
// Synthsize
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController
Search * search = [[Search alloc] init];
UINavigationController *searchNav = [[UINavigationController alloc] initWithRootViewController:search];
Nearby* nearby = [[Nearby alloc] init];
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];
Map* map = [[Map alloc] init];
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];
AboutUs* aboutUs = [[AboutUs alloc] init];
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];
Favorites* favorites = [[Favorites alloc] init];
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];
NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
You can accordingly manage in which tab you want to place navigation controller or only a view controller.
Then in each of the view controllers mentioned above you need to implement
- (id)init {}
in which you can set Tab name and image.

UIInterfaceOrientation problem

My program is not supporting the UIInterfaceOrientation. Program will not support the UIInterfaceOrientation after I add the UITabBarItem.Please give a solution. Also I added the navigationController.
Here is my code.
-(void) applicationDidFinishLaunching:(UIApplication *)application {
//I create my navigation Controller
//UINavigationController *navigationController;
//I create my TabBar controlelr
tabBarController = [[UITabBarController alloc] init];
// Icreate the array that will contain all the View controlelr
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
// I create the view controller attached to the first item in the TabBar
sivajitvViewController *firstViewController;
firstViewController = [[sivajitvViewController alloc]init];
navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
firstViewController.navigationItem.title=#"Gallery";
//viewController.tabBarItem.image = [UIImage imageNamed:#"natural.jpg"];
navigationController.tabBarItem.image = [UIImage imageNamed:#"Gallery.png"];
navigationController.tabBarItem.title = #"Gallery";
//navigationController.headerTitle = #"Some Title";
[localControllersArray addObject:navigationController];
[navigationController release];
[firstViewController release];
// I create the view controller attached to the second item in the TabBar
SecondViewController *secondViewController;
secondViewController = [[SecondViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
navigationController.tabBarItem.image = [UIImage imageNamed:#"News.png"];
navigationController.tabBarItem.title = #"News";
[localControllersArray addObject:navigationController];
[navigationController release];
[secondViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
If you have UITabBarController, all the tabs should support your interface orientation. So if you have 3 tabs and 2 of them support portrait and landscape, but the last one supports only portrait, you application will never turn to landscape.

UITabBar with UINavigationController in code

I've currently got a UITabBar with 5 screens calling UIViews. That part works fine but I want to put in a UINavigationController in a couple of them. I have found a few tutorials that work fine but all of them implement it in IB and I want to avoid that if possible.
I'm lost as to where to implement the UINavigationController, should I do it in the App Delegate with the UITabBar and call the navigation controller from the UIView or should I create it in the UIView class?
I've tried about 8 different ways and always ends up with either the Navbar not working, no nav bar at all or the app crashing.
Currently I create the tab bar like this:
tabBarController = [[UITabBarController alloc] init];
ScreenA *screenA = [[ScreenA alloc] initWithNibName:#"ScreenA" bundle:nil];
//more here
tabBarController.viewControllers = [NSArray arrayWithObjects:screenA, ...., nil];
[window addSubview:tabBarController.view];
And in the initWithNibName I have this:
self.title = #"Screen A";
self.tabBarItem.image = [UIImage imageNamed:#"someImage.png"];
Ok, do it like this...
tabBarController = [[UITabBarController alloc] init];
searchTableViewController = [[SearchTableViewController alloc] init];
UINavigationController *searchTableNavController = [[[UINavigationController alloc] initWithRootViewController:searchTableViewController] autorelease];
[searchTableViewController release];
searchMapViewController = [[SearchMapViewController alloc] init];
UINavigationController *mapTableNavController = [[[UINavigationController alloc] initWithRootViewController:searchMapViewController] autorelease];
[searchMapViewController release];
tabBarController.viewControllers = [NSArray arrayWithObjects:searchTableNavController, mapTableNavController, nil];