How to add MBProgressHUD in tabbarcontroller - ios

I had tabbarcontroller that will be called in viewcontroller1. I would like to implement MBProgressHUD(Activity Indicator) when user click on the tabbar item
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
HomeVC =[self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
TaskVC=[self.storyboard instantiateViewControllerWithIdentifier:#"TaskNCViewController"];
ShopVC=[self.storyboard instantiateViewControllerWithIdentifier:#"ShopNCViewController"];
WalletVC=[self.storyboard instantiateViewControllerWithIdentifier:#"WalletNCViewController"];
tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:HomeVC];
[tabViewControllers addObject:TaskVC];
[tabViewControllers addObject:ShopVC];
[tabViewControllers addObject:WalletVC];
[self setViewControllers:tabViewControllers];
HomeVC.tabBarItem.title=#"Home";
[HomeVC.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"home-active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"home.png"]];
[HomeVC.tabBarItem setTitleTextAttributes:#{UITextAttributeFont:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal];
TaskVC.tabBarItem.title=#"Task";
[TaskVC.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"task-active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"task.png"]];
[TaskVC.tabBarItem setTitleTextAttributes:#{UITextAttributeFont:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal];
ShopVC.tabBarItem.title=#"Shop";
[ShopVC.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"shop-active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"shop.png"]];
[ShopVC.tabBarItem setTitleTextAttributes:#{UITextAttributeFont:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal];
WalletVC.tabBarItem.title=#"Wallet";
[WalletVC.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"wallet-active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"wallet.png"]];
[WalletVC.tabBarItem setTitleTextAttributes:#{UITextAttributeFont:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal];
//set the status bar to white
[self setNeedsStatusBarAppearanceUpdate];
}

First call the delegate
your header file
#import "MBProgressHUD.h"
#interface ViewController : UIViewController<MBProgressHUDDelegate>
Then your tabbar selection or where you want to show the activity
MBProgressHUD *hud= [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
If you want to close the activity view
[hud hide:YES];

Create object of MBProgressHUD in
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
and implement UITabBarControllerDelegate

Related

What is the best way to implement UITabbarcontroller with sidepanel?

I want to implement sidebar(drawer) using uitabbarcontroller. I have implemeted for uiviewcontroller using lib JASidePanel. but I didn't find any way to implement with uitabarcontroller.
Appreciate for help
Use MFSideMenu it is available here https://github.com/mikefrederick/MFSideMenu
it is well documented and support UITabBarController.
I am using MMDrawerController as side bar together with TabBar.
This is my code for initialisation of MMDrawer and tabBar
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:MMDrawerAnimationTypeParallax];
self.tabbarController = [[UITabBarController alloc] init];
self.tabbarController.delegate = self;
JSIntroViewController *obj_loginViewController = [[JSIntroViewController alloc] initWithNibName:#"JSIntroViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:obj_loginViewController];
self.navigationController.navigationBarHidden = YES;
[self initializeTabBarControllerwithAnimation:FALSE];
UIViewController *rightViewController = [[JSMenuViewController alloc] init];
self.drawerController = [[MMDrawerController alloc]
initWithCenterViewController:self.navigationController
leftDrawerViewController:nil
rightDrawerViewController:rightViewController];
[self.drawerController setShowsShadow:NO];
[self.drawerController setRestorationIdentifier:#"MMDrawer"];
[self.drawerController setMaximumRightDrawerWidth:[UIScreen mainScreen].bounds.size.width-65];
[self.drawerController setGestureShouldRecognizeTouchBlock:^BOOL(MMDrawerController *drawerController, UIGestureRecognizer *gesture, UITouch *touch) {
if([gesture isKindOfClass:[UITapGestureRecognizer class]])
return YES;
return NO;
}];
[self.drawerController
setDrawerVisualStateBlock:^(MMDrawerController *drawerControllerr, MMDrawerSide drawerSide, CGFloat percentVisible) {
MMDrawerControllerDrawerVisualStateBlock block;
block = [[MMExampleDrawerVisualStateManager sharedManager]
drawerVisualStateBlockForDrawerSide:drawerSide];
if(block)
block(drawerControllerr, drawerSide, percentVisible);
}];
}
This is my method for tabBar initialization.
- (void)initializeTabBarControllerwithAnimation:(BOOL)animation
{
//First tab as Browse Job
JSBrowseViewController *obj_browseViewController = [[JSBrowseViewController alloc] initWithNibName:#"JSBrowseViewController" bundle:nil];
//BrowseViewController *obj_browseViewController = [[BrowseViewController alloc] initWithNibName:#"BrowseViewController" bundle:nil];
UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:obj_browseViewController];
navi1.navigationBar.translucent = NO;
//Second tab as Liked Job
LikedViewController *obj_likedViewController = [[LikedViewController alloc] initWithNibName:#"LikedViewController" bundle:nil];
UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:obj_likedViewController];
navi2.navigationBar.translucent = NO;
[self.tabbarController setViewControllers:[NSArray arrayWithObjects:navi1, navi2, nil]];
[self.tabbarController setSelectedIndex:0];
UITabBar *tabBar = self.tabbarController.tabBar;
tabBar.translucent = NO;
[tabBar setBackgroundColor:[UIColor whiteColor]];
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
[tabBarItem1 setTitle:#"BROWSE"];
[tabBarItem2 setTitle:#"LIKED"];
[tabBarItem1 setImage:[[UIImage imageNamed:#"browser-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"browser-sel-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setImage:[[UIImage imageNamed:#"like-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"like-sel-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
selectedTabIndex = 0;
self.navigationController.navigationBarHidden = TRUE;
[self.navigationController pushViewController:self.tabbarController animated:animation];
}
You can use SWRevealViewController - it's compact (just 2 files) and easy to use.

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];

How to add UITabBar with one UITabbarItem work as button?

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.

Resources