What is the best practice or good way to have with 2 tabs behave like a UINavigationController when clicking through views after the tab bar views.
Do I make a UINavigationController for each tab?
The tab bar is created like so:
// Create the tab bar text and images
AViewController *viewA = [[AViewController alloc] init];
BViewController *viewB = [[BViewController alloc] init];
UITabBarItem *tabA = [[UITabBarItem alloc] initWithTitle:#"A" image:[UIImage imageNamed:#"a.png"] tag:1];
UITabBarItem *tabB = [[UITabBarItem alloc] initWithTitle:#"B" image:[UIImage imageNamed:#"b.png"] tag:2]
viewA.tabBarItem = tabA;
viewB.tabBarItem = tabB;
NSArray* controllers = [NSArray arrayWithObjects:viewA, viewB, nil];
self.viewControllers = controllers;
For navigation you need to create UINavigationController for each like this
Updated : Try now
AViewController *viewA = [[AViewController alloc] init];
BViewController *viewB = [[BViewController alloc] init];
UINavigationController *navA = [[UINavigationController alloc]initWithRootViewController:viewA];
UINavigationController *navB = [[UINavigationController alloc]initWithRootViewController:viewA];
UITabBarItem *tabA = [[UITabBarItem alloc] initWithTitle:#"A" image:[UIImage imageNamed:#"a.png"] tag:1];
UITabBarItem *tabB = [[UITabBarItem alloc] initWithTitle:#"B" image:[UIImage imageNamed:#"b.png"] tag:2]
tabA.tabBarItem = tabA;
tabB.tabBarItem = tabB;
NSArray* controllers = [NSArray arrayWithObjects:navA, navB, nil];
self.viewControllers = controllers;
Try this
Related
I am relative new with the iOS app development. Currently I am developing a small app with a tab bar. The problem I am facing is that I would like to have different navigation items foreach tab. I tried a lot of things, but things aren't working. I am programming in the native iOS language.
In my app I've got a AppDelegate. In my AppDelegate there is a little piece of code for setting up my mainViewController:
- (void)setupOverlordViewController
{
MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navVC;
}
I am setting up my tabs in my MainViewController:
- (void)viewDidLoad
{
UIViewController *tabView1 = [[Tab1ViewController alloc] init];
UIViewController *tabView2 = [[Tab2ViewController alloc] init];
UIViewController *tabView3 = [[Tab3ViewController alloc] init];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:tabView1];
[tabViewControllers addObject:tabView2];
[tabViewControllers addObject:tabView3];
[self setViewControllers:tabViewControllers];
tabView1.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView1", nil)
image:[UIImage imageNamed:#"tabView1.png"]
tag:1];
tabView2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView2", nil)
image:[UIImage imageNamed:#"tabView2.png"]
tag:2];
tabView3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView3", nil)
image:[UIImage imageNamed:#"tabView3.png"]
tag:3];
}
Each View (tabView1, tabView2, tabView3) has there own layout, which is set in the ViewDidLoad method of the View. When I would like to add navigation buttons in the navigation bar by adding them in the ViewDidLoad method, but it seems impossible to add the buttons. The only way to add them is directly in my MainViewController, but then I can't set the navigation bar buttons different foreach tab.
The code for adding the buttons to my navigation bar is as follows:
UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(btnNewRecord)];
NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];
Could somebody explain me what I am doing wrong?
I have created a example for you by using xib files. I have created three View Controllers and added them to navigation controllers. Following the appdelegate code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *firstNavVC = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController: secondVC];
ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil];
UINavigationController *thirdNavVC = [[UINavigationController alloc] initWithRootViewController: thirdVC];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:firstNavVC];
[tabViewControllers addObject:secondNavVC];
[tabViewControllers addObject:thirdNavVC];
firstNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"First", nil)
image:nil
tag:1];
secondNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Second", nil)
image:nil
tag:2];
thirdNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Third", nil)
image:nil
tag:3];
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.viewControllers = tabViewControllers;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
return YES;
}
Following is the output :
You can download code example here
you need separate navigation controller for each tab bar view controller & then you can add UIBarButtonItem on each navigation controller.
I am trying to add Navigation bar to my tabbar view controllers.But, it is showing up in firstViewController but not in the other two views. Any reason why this is not working?
self.firstVC = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil fileName:#"firstPlist"];
self.firstVC.tabBarItem.image = [UIImage imageNamed:#"first"];
self.firstNavBarController = [[UINavigationController alloc] initWithRootViewController:self.firstVC];
self.secondVC = [[FirstViewController alloc] initWithNibName:#"SecondViewController" bundle:nil fileName:#"secondPlist"];
self.secondVC.tabBarItem.image = [UIImage imageNamed:#"second"];
self.secondNavBarController = [[UINavigationController alloc] initWithRootViewController:self.secondVC];
self.thirdVC = [[ThirdViewController alloc] initWithNibName:#"thirdView" bundle:nil fileName:#"thirdPlist"];
self.thirdVC.tabBarItem.image = [UIImage imageNamed:#"third"];
self.thirdNavBarController = [[UINavigationController alloc] initWithRootViewController:self.thirdVC];
I want a tabbarcontroller which should be inside a navigationcontroller. And i need to add it as the subview of my master page. Without ARC this code working fine. But a black screen appear when it is in ARC. help me please.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
RestaurantListingClass *restList = [[RestaurantListingClass alloc]initWithNibName:#"RestaurantListingClass" bundle:Nil];
UITabBarItem *placeTab = [[UITabBarItem alloc] initWithTitle:#"Places" image:nil tag:1];
restList.tabBarItem = placeTab;
ReOrderVC *reorder=[[ReOrderVC alloc]initWithNibName:#"ReOrderVC" bundle:Nil];
UITabBarItem *reorderTab = [[UITabBarItem alloc] initWithTitle:#"Re-order" image:nil tag:1];
reorder.tabBarItem=reorderTab;
tabBarController.viewControllers=[NSArray arrayWithObjects:restList,reorder, nil];
UINavigationController *navcontroller =[[UINavigationController alloc]initWithRootViewController:tabBarController];
DesignElementsClass *DEClass = [[DesignElementsClass alloc] init];
[DEClass setImageForsubNAvBar:navcontroller.navigationBar];
[DEClass setImageForsubTabBar:tabBarController.tabBar];
navcontroller.view.frame = view.bounds;
[self addChildViewController:navcontroller];
[self.view addSubview:navcontroller.view];
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:[NSBundle mainBundle]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
ViewOne *firstView = [storyBoard instantiateViewControllerWithIdentifier:#"ViewControllerOne"];
ViewTwo *secondView = [storyBoard instantiateViewControllerWithIdentifier:#"ViewControllerTwo"];
UITabBarItem *placeTab = [[UITabBarItem alloc] initWithTitle:#"Places" image:nil tag:1];
firstView.tabBarItem = placeTab;
UITabBarItem *reorderTab = [[UITabBarItem alloc] initWithTitle:#"Re-order" image:nil tag:1];
secondView.tabBarItem=reorderTab;
tabBarController.viewControllers=[NSArray arrayWithObjects:firstView,secondView, nil];
UINavigationController *navcontroller =[[UINavigationController alloc]initWithRootViewController:tabBarController];
[self addChildViewController:navcontroller];
[self.view addSubview:navcontroller.view];
I'm trying to find a way to layout an application that includes a tab bar on the bottom, a navigation bar on the top, and a row of buttons on the navigation bar that switches views (on the first tab).
I've drawn a very rough sketch (sorry!), but I hope it illustrates the intent.
On the bottom, there are two tabs (tab1, and tab2).
When Tab1 is selected, the navigation bar will have 3 buttons that will show different views (tab1_1, tab1_2, tab1_3).
When Tab2 is selected, the navigation bar won't show any buttons, but rather some simple text.
At this point, I have the following scheme in my application delegate's didFinishLaunchingWithOptions:
UIViewController *viewController1 = [[Tab1_ViewController alloc] initWithNibName:#"Tab1_ViewController" bundle:nil];
UIViewController *viewController2 = [[Tab2_ViewController alloc] initWithNibName:#"Tab2_ViewController" bundle:nil];
tab1NavController = [[UINavigationController alloc] initWithRootViewController:viewController1];
tab2NavController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:tab1NavController, tab2NavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
I was wondering if I need to redo how I'm doing things in order to achieve the layout as in the picture.
Any help would be appreciated, thank you!
I have done this for my current project...i hope this will help you....
At first take UITabbarController at your first viewController [first sketch you have given]
For your first view use this code....
- (void)viewDidLoad {
[super viewDidLoad];
dashBoardView = [[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
dashBoardView.title = #"dashBoardView";
UINavigationController *mydashboarController = [[[UINavigationController alloc] initWithRootViewController:dashBoardView] autorelease];
mydashboarController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:mydashboarController];
[dashBoardView release];
ordersView = [[OrdersViewController alloc] initWithNibName:#"OrdersViewController" bundle:nil];
ordersView.title = #"ordersView";
UINavigationController *myorderController = [[[UINavigationController alloc] initWithRootViewController:ordersView] autorelease];
myorderController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderController];
[ordersView release];
orderList = [[OrderListViewController alloc] initWithNibName:#"OrderListViewController" bundle:nil];
orderList.title = #"orderList";
UINavigationController *myorderListController = [[[UINavigationController alloc] initWithRootViewController:orderList] autorelease];
myorderListController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderListController];
[orderList release];
productView = [[ProductViewController alloc] initWithNibName:#"ProductViewController" bundle:nil];
productView.title = #"productView";
UINavigationController *myproductController = [[[UINavigationController alloc] initWithRootViewController:productView] autorelease];
[listOfViewControllers addObject:myproductController];
[productView release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
NSArray *segmentTextContent = [NSArray arrayWithObjects:NSLocalizedString(#"Dashboard", #""),NSLocalizedString(#"Order", #""),
NSLocalizedString(#"Product", #""),NSLocalizedString(#"More", #""),
nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 40);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
segmentedControl.tintColor = [UIColor colorWithHue:8.0 saturation:8.0 brightness:8.0 alpha:1.0];
segmentedControl.alpha = 0.8;
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
If it is not clear to you then please knock...
I hope anyone can explane me how to do this:
I have a TabBar and two TabBarItems, how can I attatch the Items to the TabBar.
I am not doing this via IB because the TabBar only fits have of the screen because the items should be on the left side.
thats how i build them:
tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController2 = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.tabBar.frame = CGRectMake(0, 974, 384, 50);
tabBarController2.tabBar.frame = CGRectMake(384, 974, 384, 50);
UITabBarItem *tbi1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
UITabBarItem *tbi2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1];
You don't set tab bar items directly in the tab bar. Instead, you assign a tab bar item to the tabBarItem property for each view controller contained by your tab bar controller. Then when you add your view controllers to the tab bar controller, the tab bar controller will manage the display of your tab bar items for you.
UITabBarController * tabBarController = [[UITabBarController alloc] init];
UIViewController * viewController1 = [[YourViewController alloc] init];
UIViewController * viewController2 = [[YourOtherViewController alloc] init];
viewController1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
viewController2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];