How to add UITabBar with one UITabbarItem work as button? - ios

I want something like looking in image, I want to do this in tabbar application or mainstoryboard. This i did normal single view based application with tabbar item.
I try following code also, in this when i click tabbar items particular viewcontroller opens. But i want on click of third tabbar item it should show UIActionSheet not third viewcontroller. How i can achieve this.
FisrtTabViewController *first = [[FisrtTabViewController alloc]initWithNibName:#"FisrtTabViewController" bundle:nil];
[first.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"myarea_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"myarea.png"]];
UINavigationController *tabItem1 = [[UINavigationController alloc] initWithRootViewController: first];
SecconTabViewController *second = [[SecconTabViewController alloc] initWithNibName:#"SecconTabViewController" bundle:nil];
[ second.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"areaconv_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"areaconv.png"]];
ThirdTabViewController *third = [[ThirdTabViewController alloc] initWithNibName:#"ThirdTabViewController" bundle:nil];
[ third.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"share_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"share.png"]];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1,second,third,nil];
[[UITabBar appearance]setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
tabbar1.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;
[self presentViewController:tabbar1 animated:YES completion:NULL];
Thanks in advance.

Related

How to change default ViewController of UITabBarController

I create basic TabBarController with 2 ViewControllers.
It worked but it have bug and i want something about it.
Problem1.
When I run (default ViewController is FirstViewController) name of SecondViewController in TabBar is not appear.
I want following this picture.
Problem2.
In my code default view is FirstViewController.
If I want to set default view is SecondViewController
by Same Tab Bar(FirstView Tab : Left ,and SecondView Tab : Right)
following this picture. How to resolve it.
This is my example code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNVC.navigationBar.barStyle = UIBarStyleBlack;
SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *secondNVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
secondNVC.navigationBar.barStyle = UIBarStyleBlack;
UITabBarController *tabController = [[UITabBarController alloc]init];
tabController.navigationItem.hidesBackButton = NO;
tabController.viewControllers = [NSArray arrayWithObjects:firstNVC, secondNVC, nil];
tabController.navigationController.navigationBarHidden = NO;
self.window.rootViewController = tabController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Thank You. ^^
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
navControl2.navigationBar.tintColor=[UIColor blackColor];
navControl3.navigationBar.tintColor=[UIColor blackColor];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl2,navControl1,navControl3, nil ];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#""] withFinishedUnselectedImage:[UIImage imageNamed:#""]];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:#"1"];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:#"tab2.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:#"2"];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:#"tab11.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:#"3"];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:#"tab5.png"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:#""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
Problem2. If I want to
set default view is SecondViewController by Same Tab Bar(FirstView Tab
: Left ,and SecondView Tab : Right). How to
resolve it.
Solution:
[self.tabBarController setSelectedIndex:1];
Problem 1:
try to set a title for the view controllers.
Problem 2:
You just set selected index of the tabBarController to 1, that should do it.
Problem1
You haven't set tabbarItem for each navigation controller, I think you should set up a UITabBarItem with title first, then assign it to tabbarItem property to navigation controller.
Problem2
Just as #Kumar says.

How to Navigate from one view to another view in ios?

I have tab bar with 4 options.
In the 1st tab, I have a button. When i click this button, I have to move from one view to another view in second tab bar.
the code I am currently using is (where shipVC is the second tab's viewController):
[shipVC.navigationController pushViewController:cartVC animated:NO];
So basically, When the button in the 1st tab viewController is pressed, I want to move from 'X' view to 'Y' view in the 2nd tab's viewController
One quick way to do this is by using NSNotificationCenter to post a notification when the button in the first tab's viewController is pressed.
Steps:
in the 2nd tab's viewController's -viewDidLoad method:
Add a notification observer and set a target selector method to it
in the 1st tab's viewController's button method:
Post the notification
Example:
Your 2nd tab's viewController class:
-(void)viewDidLoad {
[super viewDidLoad];
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doTheNavigation)
name:#"AnyNameYouWantForNotification"
object:nil];
}
-(void)doTheNavigation {
[self.navigationController pushViewController:cartVC animated:NO];
//WARNING: don't just push, put a check or else repeated click on the
//button in the 1st tab will push cartVC again and again.
//This will not only warrant a crash but look very ugly
}
Button method in the 1st tab's viewController class:
-(IBAction)someBtnClick:(UIButton *)sender {
//...
//you can use this to post a notification from anywhere now
[[NSNotificationCenter defaultCenter] postNotificationName:#"AnyNameYouWantForNotification"
object:nil];
//...
}
So...
When the button in the 1st tab is clicked, the button action (that I named someBtnClick) will post a notification with the name AnyNameYouWantForNotification
The 2nd tab (should be already loaded and ready) will be listening for a notification that has AnyNameYouWantForNotification as it's name.
When this notification is received, it will execute the linked selector method (that I named doTheNavigation)
check my answer
-(void)settabbar{
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
navControl2.navigationBar.tintColor=[UIColor blackColor];
navControl3.navigationBar.tintColor=[UIColor blackColor];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl1,navControl2,navControl3, nil ];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#""] withFinishedUnselectedImage:[UIImage imageNamed:#""]];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:#"view1"];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:#"tab2.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:#"view2"];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:#"tab11.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:#"ciew3"];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:#"tab5.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:3] setTitle:#"Maintain Customer"];
[[[[self.tabBarController tabBar] items] objectAtIndex:3] setImage:[UIImage imageNamed:#"tab4.png"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:#""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController*)viewController{
NSLog(#"select : %d",tabBarController.tabBar.selectedItem.tag);
if(self.tabBarController.tabBar.selectedItem.tag==0){
[[self.tabBarController.viewControllers objectAtIndex:0] popViewControllerAnimated:YES];
}else if(self.tabBarController.tabBar.selectedItem.tag==1){
[[self.tabBarController.viewControllers objectAtIndex:1] popViewControllerAnimated:YES];
}else if(self.tabBarController.tabBar.selectedItem.tag==2){
[[self.tabBarController.viewControllers objectAtIndex:2] popViewControllerAnimated:YES];
}else {
[(UINavigationController*) viewController popToRootViewControllerAnimated:YES];
}
return YES;
}
You need to also make application with Naigationcontroller in your appdelegate
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
HomeViewController *homeView = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView animated:YES];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:homeView];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
after that your navigationbar will appear in your first viewcontroller then you can navigate.

NavigationController Pushing TabBarViewController

I have noticed that my GoogleMap stopped working in the sense that it has become inactive after first touch. This got me thinking, so I decided to create a new project with the same code to check to see if it was the code and shows that its not my GoogleMaps code. I think it has to do with how I set up my Navigation Bar and Tab Bar.
This is what I have. I have an initial ViewController inside a NavigationController. This view controller does many things, one of which is push view controllers. I have a button that pushes a UITabBarController onto the NavigationController stack. I have created a UITabBarController inside storyboard and gave that controller a StoryboardID: GRxTabBarViewController. GRxTabBarViewController has 4 view controllers, PricesViewController, SavingsViewController, InfoViewController, and MapsViewController.
I then created the header and implementation files of these controllers. This is what I have in my initial view controller to push this tab bar controller and tab bars.
-(void)pushTabBar
{
UIStoryboard *iPhoneStoryBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
GRxTabBarViewController *tabViewController = (GRxTabBarViewController*) [iPhoneStoryBoard instantiateViewControllerWithIdentifier:#"GRxTabBarViewController"];
PricesViewController *pricesController = [tabViewController.viewControllers objectAtIndex:0];
SavingsViewController *savingsController = [tabViewController.viewControllers objectAtIndex:1];
InfoViewController *infoController = [tabViewController.viewControllers objectAtIndex:2];
MapsViewController *mapController = [tabViewController.viewControllers objectAtIndex:3];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:infoController];
UINavigationController *savingsNav = [[UINavigationController alloc] initWithRootViewController:savingsController];
UITabBarItem *tabBarItem1 = [[tabViewController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-prices-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-prices-normal"]];
UITabBarItem *tabBarItem2 = [[tabViewController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-savings-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-savings-normal"]];
UITabBarItem *tabBarItem3 = [[tabViewController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-info-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-info-normal"]];
UITabBarItem *tabBarItem4 = [[tabViewController.tabBar items] objectAtIndex:3];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:#"tabbar-map-selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tabbar-map-normal"]];
[tabViewController setViewControllers:[NSArray arrayWithObjects:pricesController, savingsNav, navController, mapController, nil]];
[self.navigationController pushViewController:tabViewController animated:YES];
}
GRxTabBarViewController is a UITabBarController and has this in the implementation file.
GRxTabBarViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *backButton = [[UIButton alloc] init];
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(dismissTab) forControlEvents:UIControlEventTouchUpInside];
backButton.frame = CGRectMake(0, 0, 20.0f, 44.0f);
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
self.navigationItem.hidesBackButton = YES;
}
-(void)dismissTab
{
[self.navigationController popViewControllerAnimated:YES];
}
Am I doing anything wrong? Am I setting up my tab bar incorrectly? Has someone pushed a tab bar before and have any ideas what I am doing wrong that can help me out? Any guidance or help is appreciated. Thanks in advance!

Load all Viewcontrollers when application opens

I'm working on a tabbed application for iOS. Because the tabbar icons are loaded in each viewcontroller separately, I was wondering if it is possible to load all the viewcontrollers at once in the background so all the tabbar icons are loaded when the application is launched.
The tabbar makes use of two icons for each tabbar item (selected and unselected), thats why I choose to load the icons in each viewcontroller separately
And otherwise, is there a possibility to load the tabbar icons in the App delegate?
Yes it is possible. You can initialize all controllers and all images in App delegate's didFinishLaunchingWithOptions methods. Here is the example:
UIViewController *locateTabController = [[LocationTabController alloc] initWithNibName:#"LocationTabController" bundle:nil];
UINavigationController *locationTabNavigationController = [[UINavigationController alloc] initWithRootViewController:locateTabController];
// Product Tab
UIViewController *productsTabController = [[ProductsTabController alloc] initWithNibName:#"ProductsTabController" bundle:nil];
UINavigationController *productsTabNavigationController = [[UINavigationController alloc] initWithRootViewController:productsTabController];
// Delivery Tab
UIViewController *nextDeliveryTabController = [[DeliveryTabController alloc] initWithNibName:#"DeliveryTabController" bundle:nil];
UINavigationController *nextDeliveryTabNavigationController = [[UINavigationController alloc] initWithRootViewController:nextDeliveryTabController];
// Order Tab
UIViewController *standingOrderTabController = [[OrderTabController alloc] initWithNibName:#"OrderTabController" bundle:nil];
UINavigationController *standingOrderTabNavigationController = [[UINavigationController alloc] initWithRootViewController:standingOrderTabController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationTabNavigationController, productsTabNavigationController,nextDeliveryTabNavigationController, standingOrderTabNavigationController, nil];
Here navigation controller is provided as every tab controller class had its own navigation.
You can add titles and images at the same time.
/// Adding titles on each of the tab bar controllers
[[self.tabBarController.viewControllers objectAtIndex:0] setTitle:#"Locate"];
[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"LocateIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"LocateIconInactive.png"]];
[[self.tabBarController.viewControllers objectAtIndex:1] setTitle:#"Products"];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"ProductsIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ProductsIconInactive.png"]];
[[self.tabBarController.viewControllers objectAtIndex:2] setTitle:#"Delivery"];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"NextDeliveryIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"NextDeliveryIconInactive.png"]];
[[self.tabBarController.viewControllers objectAtIndex:3] setTitle:#"Order"];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"StandingOrderIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"StandingOrderIconInactive.png"]];
Sub class your UITabBarController and set the images on the viewWillAppear. I found that this is the cleanest way of doing it.
You dont need to load any viewcontroller when application opens for this purpose. All you have to do is set tabbar icon inside each viewcontroller's initialization method like below:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:IMAGE_NAME];
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:IMAGE_NAME] withFinishedUnselectedImage:[UIImage imageNamed:IMAGE_NAME]];
self.tabBarItem.title = TITLE;
}
return self;
}
After that when you initialize the viewcontroller object from the AppDelegate, tabbar icon will be set.
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];

colour icon in UITabbar item objective-c

I have a UITabbar controller with 3 item, I want to have colour icon instead of based Gary icons,
would you please give me some hint that how can I have colour icon in tababr,
Here is my code:
self.title = #"test";
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage
imageNamed:#"test"] tag:0];
normally test is a icone with a colour picture, but in UITabbar it's just Gary,
Thanks in advance!
Use this code:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"test"]
withFinishedUnselectedImage:[UIImage imageNamed:#"test"]];
You will need to build your own TabBarController. As Per the Apple docs on the matter "This class [UITabBarController] is not intended for subclassing". The docs on the UITabBarItem say that when you are supplying an image for the tab bar "The images displayed on the tab bar are derived from this image". So, whatever image you provide to the tab bar will get manipulated to make it conform to the "normal" look of a tab bar image.
So, you can build a UIViewController with some UIButtons as subviews and then manage the entire look and feel that way.
you have to create 2 images for all 3 tabs one is unselected and second is selected.
after that in your appdelegate.m file write below code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.searchArray = [[NSMutableArray alloc]init];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
viewController1 *view1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
view1.tabBarItem.image = [UIImage imageNamed:#"tab-selected-1"];
view1.tabBarItem.title = #"Title1";
viewController2 *view2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
view2.tabBarItem.image = [UIImage imageNamed:#"tab-selected-2"];
view2.tabBarItem.title = #"Title2";
viewController3 *view3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
view3.tabBarItem.image = [UIImage imageNamed:#"tab-selected-3"];
view3.tabBarItem.title = #"Title3";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view3, nil];
UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-1.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-1.png"]];
UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-2.png"]];
UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-3.png"]];
return YES;
}
try this your problem surly solved. Best Of Luck

Resources