How to create a tab bar on iOS? - objective-c

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

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

How to make a tab bar controller
by me
Drag tab bar controller into storyboard (hopefully you have one)
Control-drag from tab bar controller to each view you want hooked up to it
Pop bottles
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.

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).

Related

How to specify the left, right, or title View button in XIB

I do not want to create a custom navigation bar.
I am pushing a UIViewController and I want to customize how the navigation bar looks for that UIViewController
In story board, we just specify the segue and a nav bar show up on the screen. We just drag and drop UIBarItem to the left and right.
In XIB, the navigationBar simply doesn't show up.
I can add navigation Item but the one I added is ignored.
I've heard that there used to be an outlet called navigationItem but it's deprecated for reason I do not know.
I can add UINavigationBar, however that would be adding my own custom bar. I want the navBar that's provided by UINavigationController.
The appearance of the Navigation Bar in the storyboard designer is just there to illustrate how your screen will look when you load your ViewController inside a UINavigationController. It doesn't mean that you actually have a navigation controller in your app.
You need to add a UINavigationController to your storyboard (probably as the first scene), and then connect your ViewController to it (as the root view controller).
Then you should be able to set your title in the storyboard designer, and drag bar button items onto the navigation bar.
See also the answer to this question.
You can setup it in code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Title";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back"style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItem = "Create button" ;
self.navigationItem.leftBarButtonItem = "Create button" ;
self.navigationItem.titleView = "Create custom title view";
}
I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

How to get the Xcode TabView default black bar in bottom?

In a new tabbed app application when two more views are added, views which are created by default has a black bar on bottom and the two views that are manually created doesn't have that black bar ?
How to enable that black bar ?
You need to connect your two other scenes to the tab bar controller. Hold the control key down, and drag from Tab Bar View Controller to the other View Controller. When it asks what type of a segue you want to create, choose "view controllers" under the "Relationship" segue section.
If you are using NIBs, drag a view controller object from the object library into the tab bar controller. Once it's there, select the new view controller in the view controller explorer and configure the Custom Class in the Identity Inspector.
... and the name of the nib in the Attributes Inspector.
In your app delegate you should see code like this:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
Modify it to include your third and fourth view controllers. Otherwise, how would your tab controller know your new view controllers exist!

Add a XIB view to a tab view in the storyboard

I am working on an Iphone application.
I am using a StoryBoard.
I have a Tab View with 3 tabs. "Home", "Users" and "Settings".
I create the "Home" and "Users" view on the story board, but The settings view is a XIB file (SettingsView.xib)
How can I make the third tab ("Settings") open the SettingsView.xib? Can I use both the story board and xib files?
I tried to initialize a UINavigationController in the startApp method in the AppDelegate but I can't find out how to add it to the story board.
Thanks for any help
TabViewControllers usually have one navigation controller for each tab.
Create the navigation controllers in storyboard and connect them to the navigationcontrollers relation of the tab view controller.
The initial view of the navigation controller connects to the rootViewController relationship of the navigation controller.
As to your second question, I'm not certain, but I think the following will work:-
Create a UIViewController in storyboard and change it's class to your class that you're loading from an XIB. When the storyboard instantiates the class, it will use the XIB provided the class name of the class exactly matches the name of the XIB. I don't think you can do any iPad/iPod checking here though.
You can add a xib-based view to your storyboard-based tab bar controller as follows. I am assuming the following:
The tab bar controller is the initial view controller of your storyboard.
Your settings controller is a class called SettingsController
You have a tab bar image in your bundle called SettingsTabImage
Define the tab bar controller in the storyboard with just your storyboard-based tab bar items in it - Home and Users in your case
In your application delegate, use the following code in application:didFinishLaunchingWithOptions::
// Create your settings view controller
SettingsController *settingsVC = [[SettingsController alloc] initWithNibName:nil bundle:nil];
// Create a tab bar item
UITabBarItem *settingsItem = [[UITabBarItem alloc] initWithTitle:#"Settings" image:[UIImage imageNamed:#"SettingsTabImage" tag:0];
settingsVC.tabBarItem = settingsItem;
// Get a reference to the tab bar controller
UITabBarController *tbC = (UITabBarController*)self.window.rootViewController;
// Get the current view controllers in your tab bar
NSMutableArray *currentItems = [NSMutableArray arrayWithArray:tbC.viewControllers];
// Add your settings controller
[currentItems addObject:settingsVC];
tbC.viewControllers = [NSArray arrayWithArray:currentItems];

how to use multiple tab bar controller in an application?

I have several buttons in root view and I want to switch different tab bar controller when click each button. How can I do this?
I did for just first 'Denetim Formu' button. When click it it show second picture. But How can I do like this for other 'Lokasyon, Taahhüdname, Form Sorgulama, ect' buttons. Each have to show different tab bar controller.
What you can do is on your particular button click:-
1.)Which tab bar button item and its view controller you want to change(identify it).
*YourViewController* *import=[[[*YourViewController* alloc]initWithNibName:#"*YourViewControllerNibName*" bundle:nil] autorelease];
UINavigationController *baseNav = [[[UINavigationController alloc] initWithRootViewController:import]autorelease];
NSArray *arr=[[appDelegate tabBarController]viewControllers];
NSMutableArray *array=[NSMutableArray arrayWithArray:arr];
[array replaceObjectAtIndex:1 withObject:baseNav];
[[appDelegate tabBarController]setViewControllers:array];
appDelegate.tabBarController.selectedIndex=1;
UITabBarItem *item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:1];
item.image=[UIImage imageNamed:#"hometab.png"];
item.title=#"Home";
By this way you can change your tab bar view controller tab with its item name and image.

Top Bar does not appear for presentModalViewController

I've created a UIViewController subclass called addItemToListViewController. I selected add an "xib" as well, and just created a simple page with a couple of labels and a textField. In the interface builder I selected "Top Bar - Navigation Bar" so that when it is put on the stack when the application runs it will have a top bar that will match the initial main window. In the Interface builder it shows the top border, but when I run the application in the simulator the top bar is not present once the view is displayed.
Here is the code I placed in the rootViewController to present the view controller
- (IBAction)addButtonPressed:(id)sender
{
AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: #"AddItemToListViewController" bundle:nil];
[self presentModalViewController: AddItemToListViewController animated: YES];
[AddItemToListViewController release];
}
I'm only able to have the top bar present if I manually add a Navigation bar to the xib. If I must add a Navigation bar to my xib, what is the purpose of the "Top Bar" attribute?
- (IBAction)addButtonPressed:(id)sender
{
AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: #"AddItemToListViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addItemToListViewController];
[self presentModalViewController: navController animated: YES];
[AddItemToListViewController release];
[navController release];
}
That "top bar - Navigation bar" in InterfaceBuilder is what's known as a "Simulated Metric". It's there to help you lay out your view with correct spacing when other visual elements - the status bar, navigation bar, or tab bar - might consume some of the device's screen real estate. It doesn't actually do anything other than shrink the vertical dimensions of the view defined by the NIB. The purpose is to help you layout your view, not to actually create a component that will appear in your app.
If you want a navigation bar, then you have two choices. The first choice is to use a navigation controller (of which your initial view will have to be the root) and call
[self.navigationController pushViewController:newVC animated:YES];
The process of setting up a navigation controller correctly, etc, is nontrivial, and you should do some searching to find the best way to do that for your app. For a simple app, especially if you're just learning iOS, you can use the "Navigation-based Application" template when you create a new project. With a navcon, you get all the fancy behavior normally associated with that top bar - an automatic back button, fancy left/right scrolling when you transition to a detail view, etc.
The second option is to put a "fake" navigation bar in the detail view, using the Navigation Bar object. You can find that object, plus some other related objects, in the bottom half of the "Utilities View" (the right-most pane) in XCode. Just drag the object into your XIB and blammo, you have a 44-pixel tall gray bar. This navigation bar is just like what you get when you use a Navigation Controller except you don't get the stack functionality; you can still add buttons to the left and right, change the title, tint it to a specific color, etc.
The xib does not know you will use the controller as a modal view as it could also be used for a normal view which could show a top bar. Only when you push the view it will use or ignore the showing of this top bar.
In short: its there in case you will use the xib for a normal view :)