How to Navigate from one view to another view in ios? - 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.

Related

How to add MBProgressHUD in tabbarcontroller

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

How to make a tab bar application out of standalone NIBs?

I have 5 standalone table view controller nibs (with custom cell implementation) accessible through an another table view menu list (no storyboards)
The client desires to have all 5 nibs in tabs. So I need to get rid of the menu list and provide nibs in TABs .
how can I do this ?
First add this property to your AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
make a method to set the views and set up your tabbar like:
-(void)setViews
{
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] ;
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
UINavigationController *navigationController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
[navigationController1.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
[navigationController2.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
[navigationController3.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
[navigationController4.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
[navigationController5.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
[navigationController1.navigationBar setHidden:YES];
[navigationController2.navigationBar setHidden:YES];
[navigationController3.navigationBar setHidden:YES];
[navigationController4.navigationBar setHidden:YES];
[navigationController5.navigationBar setHidden:YES];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar setBackgroundColor:[UIColor clearColor]];
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar"];
[[[self tabBarController]tabBar]setSelectionIndicatorImage:[UIImage imageNamed:#"transparent.png"]];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = #[navigationController1, navigationController2,navigationController3,navigationController4,navigationController5];
self.window.rootViewController = self.tabBarController;
}
avoid the set images and setHidden if you don't want to or not want to make the custom navigation bar.
and call this method in your didFinishLaunchingWithOptions.
Now set up the delegate method for tabbar and you can set the custom images over there:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 0)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-1"];
}
else if (tabBarController.selectedIndex == 1)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-2"];
}
else if (tabBarController.selectedIndex == 2)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-3"];
}
else if (tabBarController.selectedIndex == 3)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-4"];
}
else if (tabBarController.selectedIndex == 4)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-5"];
}
}
I'd set it all up in the main nib/storyboard, but it's easier to show in code. You can create the view controllers in the usual way (again, nib or storyboard or code).
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:#[vc1, vc2, vc3, vc4, vc5] animated:YES];
UIWindow *window = [[UIApplication sharedApplication] delegate].window;
[window setRootViewController:tabBarController];

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.

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!

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