2

I need on my main view controller to have a tab bar with tabs to navigate to all my other controllers. I just need the tab bar on this controller and when i get to another controller i just need to have a back button to go to the main controller. Now i have some questions. I created the tab bar in the main view controller and all the tabs with the text and images that i need. However i see that i can only create IBOutlet for the tab bar and not IBActions for every tab(as i thought). So i created an IBOutlet and connected it to my tab bar.

How can i refer to every tab?

If i can refer to every tab how is it possible to change the view controller when a tab is selected when i cant use an action about it?(I am not asking for the code to change controllers , i am asking for the place that i should put the code so that my application knows that this specific tab was pressed and has to change controller). Thank you for reading my post :D

user1511244
  • 725
  • 3
  • 11
  • 15
  • Use a tab bar controller, it should handle everything like this by default. Much easier than adding a tab bar to a view controller. – Dustin Jul 11 '12 at 12:52
  • I read some tutorials on this and seemed hard... – user1511244 Jul 11 '12 at 13:12
  • Not harder than making everything yourself to duplicate the function that a tab bar controller has. If you just want something you can press to go to other controllers, then just make some buttons hooked up to segues. That requires zero code. Otherwise, use the tab bar controller. – Dustin Jul 11 '12 at 13:14
  • Ok do u have a good and straightforward tutorial to propose? maybe the ones i am reading are complicated... – user1511244 Jul 11 '12 at 13:18
  • also something important! when i hit a tab i dont want to navigate to a new nib file that is associated with the current controller! i want to navigate to a complete new controller with its own nib file. i am just saying cause all the tutorials i am reading are about just changing nib files that are associated it with the current controller.. – user1511244 Jul 11 '12 at 13:26

3 Answers3

9

You can create a UITabBarController programmatically in applicationDidFinishLaunching and set it as the root view controller (or if you prefer, you can present it as a modal view). Here is the minimal code to do it:

UITabBarController *tabBarController = [[UITabBar alloc] init];

UIViewController *controller1 = [[YourViewController alloc] init];
UIViewController *controller2 = [[YourOtherViewController alloc] init];

tabBarController.viewControllers = [NSArray arrayWithObjects:
    controller1,
    controller2,
    nil];

// set as the root window
window.rootViewController = tabBarController;

If you want to customize the look of the tab bar items, do so by adding overloading (UITabBarItem *)tabBarItem in the child view controller(s):

- (UITabBarItem *)tabBarItem
{
    return [[UITabBarItem alloc] initWithTitle:@"Amazing" image:[UIImage imageNamed:@"Blah.png"] tag:0];
}
Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29
1

How to make a tab bar controller by me

  1. Drag tab bar controller into storyboard (hopefully you have one)
  2. Control-drag from tab bar controller to each view you want hooked up to it
  3. Pop bottles

enter image description here

Just so you know, this gives you the default tab bar controller behavior (so it will always be present and you can click from any page to another). If that's not what you want, then don't use a tab bar controller. To do otherwise is an abomination.

Dustin
  • 6,783
  • 4
  • 36
  • 53
  • i dont have a storyboard and i dont want to use one. I only want the tab bar to be shown on my main view controller so that i can navigate to other controllers! On the other controllers i ll just have a back button to return to the main controller. Thank you very much for trying to help really appreciate it :) . Do you have any idea how i can do what i am describing in the simplest way? I guess there should be a function or smth that is associated with the tab bar i created so that when u hit a tab the code runs there and depending on the tab u hit u do the corresponding action right? – user1511244 Jul 11 '12 at 13:32
  • Storyboards are the best, you should use them. If you want to do everything programmatically, then read http://stackoverflow.com/q/3923465/1487063. What you want the tab bar to do is kind of frowned upon by Apple http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html. To get what you want in a way that won't get rejected, just add some buttons and use `presentModalViewController`. For a back button, use `dismissModalViewController` – Dustin Jul 11 '12 at 13:39
  • pff i cant believe its so hard to do this. the stackoverflow link doesnt exactly show what i am asking :\ – user1511244 Jul 11 '12 at 13:55
  • 2
    Everything is hard without storyboards. Everything but curling up into a ball and crying softly to yourself until they come to take you away. You should use them. – Dustin Jul 11 '12 at 14:01
  • I need to support iOS versions that dont use them.. ahhaha great comment by the way! – user1511244 Jul 11 '12 at 14:10
  • Well look at this code https://sites.google.com/site/iphonesdktutorials/sourcecode/TabBarControllerTutorial.zip?attredirects=0 Just so you know, iOS5 has an 85% adoption rate and if you're new to coding, just don't support anything older. We're only a few months away from iOS6, so there's a very, very small chance supporting older iOS systems is significant. My company dropped iOS < 5 support several months ago. – Dustin Jul 11 '12 at 14:13
  • yes and i completely agree with u but my boss has a different point of view :D – user1511244 Jul 11 '12 at 14:18
  • Well look at that source code, show your boss some numbers, and good luck. – Dustin Jul 11 '12 at 14:18
  • Thanks a lot Dustin for all your help :) – user1511244 Jul 11 '12 at 14:20
  • Dustin a last question so that i dont search for nothing. With a tab view controller i can switch between controllers right? Not just between nib files that are associated with the same controller? – user1511244 Jul 11 '12 at 14:22
  • You can. Read the apple doc on the subject http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/TabBarControllers.html Switching between different view controllers is the ONLY thing it can do. – Dustin Jul 11 '12 at 14:23
-1

Storyboards are definitely helpful, but if you don't want to use one that's fine. Doing the Control Drag from the Tab Bar Controller to your new View Controller does indeed work (Dustin's response).