How to put a Table View Controller into a Tab Bar Controller? - iphone

I'm starting out with learning Table Views. My applications uses a Tab Bar Controller and all the tabs are simple views. I want to add another tab, and put a Table View inside of it..how do i do that?
Thanks a lot!

If I want to add another view controller to a tab bar controller, I usually do something like this:
NSMutableArray *items = [tabBarController.viewControllers mutableCopy];
MyTableViewController *c = [[MyTableViewController alloc] init];
[items addObject:c];
[c release];
tabBarController.viewControllers = items;
[items release];

It depends on how you have created the tab bar controller. If it was designed in IB, then drag another tab bar item over to the main window. I then embed a view controller within the tab bar and assign it to the tableview class in question. The .h file for the class will need to be written:
#interface myClass : UITableViewController <UITableViewDelegate, UITableViewDataSource>
The other method that Apple uses when you first set up a basic tab bar app, handles it programmatically with the app delegate. See:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
for the relevant code.

Related

How to add tab bar controllers to the root view of the split view controller application

I am very new to the iPad UISplitViewController.
I need to add a tab bar containing 3 tabs at the bottom of the master view / left view. I have a different View Controller for each of the 3 tabs. I haven't found any other examples of Tab bars being used in split view based applications.
Where do I insert a tab bar controller for displaying at the bottom of the the root view?
How do I establish a connection so that when I select a table cell, the detailItem info gets displayed in the Detail View? Tab bar was added to the Root View in IB. Should I be adding it programmatically instead?
in your app delegate add tabbar controller then add your view controllers to tabbar controller and set the window rootview controller to tabbar controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.tabbar=[[UITabBarController alloc] init];
self.vc1 = [[vc1 alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
// do the same to other VCs
self.tabbar.viewControllers=[NSArray arrayWithObjects:vc1,vc2,vc3,vc4, nil];
self.window.rootViewController = self.tabbar;
[self.window makeKeyAndVisible];
return YES;
}
i hope it helps :-)
you have to take UITabBarController dynaically.
In .h file
UITabBarController *tabBar;
in .m file
create objects to your classes in appDidFinish Launch
For example you have
Class1 and Class2
in appDidFinishLaunch
Class1 *obj1=[Class1 alloc]initWithNibName:#"Class1" bundle:nil];
**Class2 obj2=[Class2 alloc]initWithNibName:#"Class2" bundle:nil];*
// Master navigation controller by defaults comes with template code
// Now you have create Array for tabBar
NSArray *tabViewArray=[[NSArray alloc] initWithObjects:obj1,obj2,masterNavigationController, nil];
tabBar=[[UITabBarController alloc] init];
[tabBar setViewControllers:tabViewArray];
// now you have to edit the statement which contains splitview.viewArray repalce masterNavigataionControler with tabBar
self.splitViewController.viewControllers = [NSArray arrayWithObjects:tabBar, detailNavigationController, nil];
Try this i hope it will helps you.
All you have to do is initialize the first argument of uispliviewcontroller for view as tabbar instead of a view or you can use uisegmentedcontrol.

Accessing view controllers programmatically

New to iOS development, I've been following the tutorials on developer.apple.com, and am now adding functionality to those examples to further my knowledge.
The "second ios app" tutorial gives you a navigation controller based app. Extending this app, I want to have a tab bar controller as the first view controller.
So I now have the following setup:
All good. But there is code in the BirdsAppDelegate (a UIApplicationDelegate) which was relying on the navigation controller being the root view controller, so it can create and assign the "datacontroller" object.
This is the original code (before I added the tab bar controller):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
BirdsMasterViewController *firstViewController = (BirdsMasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init];
firstViewController.dataController = aDataController;
return YES;
}
Now this code fails because it assumes the root view controller is the navigation controller.
I have updated the code so that it works - but in my opinion it is ugly, and would have to be changed every time I make a change to the view controller hierarchy:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = (UINavigationController *) [[tabBarController viewControllers] objectAtIndex:0];
BirdsMasterViewController *firstViewController = (BirdsMasterViewController*) [[navigationController viewControllers] objectAtIndex:0];
BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init];
firstViewController.dataController = aDataController;
return YES;
}
So my question is: What is the better way to do what I am doing in the code above, so that any changes to the hierarchy will not break the code?
How do I programmatically access the view controller I am after in the application delegate, so that I can create and assign it's BirdSightingDataController object?
Thanks!
You can loop the [navigationController viewControllers] array looking for an instance of BirdsMasterViewController... Using [obj isKindOfClass:[BirdsMasterViewController class]].
You don't even need that code at a all. If you just want to change the controller, go to the storyboard and select the viewController you want to change to a TabBarController. In the Editor menu, there is an option for "Embed In", the choices are TabBar and Navigation controllers.
I always start with a single view application template. There is no code in the "application didFinishLaunchingWithOptions:" method,(except to return YES). You can set any viewController as your initial view in the storyboard, by setting the is initial View Controller check box, or just dragging the arrow to the viewController you want as your initial view.

how to structure my objects when using uinavconttroller and uitabbar

I am trying to understand what is a proper structure of the objects when using uinavigationcontroller with a tab bar.
I want my app to have the following structure: welcome/login screen -> 3 bar tabs.
I have the following objects/classes:
AppDelegate
WelcomeViewController
TabController
FirstTab
SecondTab
ThirdTab
I have also created a uinavcontroller under WelcomeViewController once the user clicks on "enter" to the app:
-(IBAction)aMethod:(id)sender {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstView *controller = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController = self.navigationController;
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
}
My question is - how should I manage the tab bar - where should I declare each one of its pieces, and will I need to create a uitabbarcontroller in this case (in which case, where?)).
I am very confused as to how to place the different tab bar related declarations and none of the examples/ tutorials our there were able to clarify this for me.
BTW - I started this app from a view based application.
Thanks!
You can either set this up in code or you can do it using interface builder.
I prefer the interface builder method as you can visually see the structure of your view controllers.
This is how I do it...
In your AppDelegate.h add a property
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
In your AppDelegate.m firstly synthesize the property
#synthesize tabBarController = _tabBarController;
Set up the application:didFinishLaunchingWithOptions: method to look something like this (you may do more work in this method)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:self.tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
In MainWindow.xib drag a Tab Bar Controller object onto your objects area (this is where your AppDelegate and Window objects are).
Ctrl + Drag from the AppDelegate to the Tab Bar Controller object and select the property that we just made.
NOTE: Now we have a Tab Bar Controller set up and ready to roll.
There should be two tabs set up as an example. If you just want to use sub classes of UIViewController then just change the classes of these objects to represent your UIViewController sub classes.
If you want to use UINavigationController then drag a UINavigationController object onto your Tab Bar Controller object.
Now click the disclosure triangle on UINavigationController and change the class of its ViewController to be your custom subclass of UIViewController.

Put View Controller in TabBar ViewController

I am Create already three view controller & i want to just add those view controller in tabBarview controller.
The view controller is below
First --> Login Page
Second --> Tabbar View controller
1)---> Employee View controller
2)---> Task View Controller
3)----> Home View controller
I am create above three view controller separate. I want add those in tab bar controller using Interface Builder or coding.
You can find more descriptive example from Apple docs - Combined View Controller Interfaces
I assume that Login Page is your root view controller here. Where _tabBar, _window and _loginvVewController are globally declared in the appDelegate headers files. You can also take _loginvVewController locally inside the didFinishLaunchingWithOptions method depend upon your requirements.
AppDelgate.h
UIWindow *_window;
UITabBarController *_tabBar;
LoginViewController *_loginvVewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.loginvVewController = [[LoginViewController alloc] init];
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.loginvVewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)initializeTabbar {
/*
* Set up controllers for the tab bar controller
*/
EmployeeViewController *vc1 = [[[EmployeeViewController alloc] initWithTitle:#"View 1"] autorelease];
TaskViewController *vc2 = [[[TaskViewController alloc] initWithTitle:#"View 2"] autorelease];
HomeViewController *vc3 = [[[HomeViewController alloc] initWithTitle:#"View 3"] autorelease];
// View Controller with each Navigational stack support.
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:vc1];
/*
* Set up tab bar controller
*/
self.tabBar = [[UITabBarController alloc] init];
self.tabBar.viewControllers = [NSArray arrayWithObjects:navController, vc2, vc3, nil];
[self.window addSubview:self.tabBar.view];
}
In my quick hackathon for this problem, I have taken the button "Click here!" on login page - once you click on it will navigate you inside the app with tabbar. If you need the sample project then email me at d3minem#gmail.com.
After many requests via email - I have created the demo project and upload here. https://github.com/Deminem/SimpleTabbarApp--iPhone-
Please vote if you find it useful.
Good luck!
The better way would be to create a TabBarContoller based application and add the ViewControllers to the TabbarViewController.
The path is straight forward.
In IB, place your tabbar controller where you like it, make it have three pages and set their view controller classes.
Or in code, just add the tab bar controller with those three controllers set as its view controllers.

How to add navigationController to existing tab application?

I have an iPhone application with 2 tabs. This is my application delegate:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
My first tab is a subclass of UITableViewController. I would like to push a new view controller when someone clicks on a row of my table. I know that this is done in didSelectRowAtIndexPath like this:
TableViewDetailViewController *fvController = [[TableViewDetailViewController alloc] initWithNibName:#"TableViewDetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:fvController animated:YES];
[fvController release];
fvController = nil;
My self.navigationController is NIL so nothing gets pushed to the view stack. Where should I create my navigationController, so there would be as little code to change as possible?
We have direct solution using Interface builder.
In interface builder go to xib in which you have added tabBarController and delete the tabBarItem with which you have connected your tableViewController .
Now, drag and drop an UINavigationController from library in your tabBar and set this navigationController's rootViewController as your tableViewController.
Thanks
Instead of setting your TableViewDetailViewController as one of your tabbbars ViewControllers, you should create an UINavigationController for each tab on your tabbar. Then place your TableViewDetailViewController inside the corresponding UINavigationController. self.navigationController will point to a valid UINavigationController in that case.