I have been working on a UINavigationController based application, using a UITableView for the rootview and Core Data for the data source.
Unfortunately I didn't plan ahead very well and now would like to implement tab bar navigation to the app on top of what I already have.
Can anyone recommend a simple way to do this? Or am I better off starting again with the TabBar based template and try to plugin my existing code?
Thanks guys!
Adam
Rather than start over, you may just want to create a tab-bar-based app and look at the code it produces, then try to do the same thing in your app.
You probably just need to change your application delegate's application:didFinishLaunchingWithOptions: method to look something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *viewControllers = [[[NSMutableArray alloc] init] autorelease];
// First tab
MyController *myController = [[[MyController alloc] init] autorelease];
mapViewController.navigationItem.title = #"First";
UINavigationController *myRootController = [[[UINavigationController alloc]
initWithRootViewController:myController]
autorelease];
myRootController.tabBarItem.title = #"First";
myRootController.tabBarItem.image = [UIImage imageNamed:#"MyControllerTab"];
[viewControllers addObject:myRootController];
// Second tab
MyOtherController *myOtherController = [[[MyOtherController alloc] init] autorelease];
myOtherController.navigationItem.title = #"Second";
UINavigationController *otherRootController = [[[UINavigationController alloc] initWithRootViewController:myOtherController] autorelease];
otherRootController.tabBarItem.title = #"Second";
otherRootController.tabBarItem.image = [UIImage imageNamed:#"OtherControllerTab"];
[viewControllers addObject:otherRootController];
// Create other tabs
// ...
// Tab bar
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
[tabBarController setViewControllers:viewControllers];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
If you want to plug in your existing navigation controller to one of the tabs on a tab bar, it's pretty simple - just define a new UITabBarController to serve as the root controller and have it load your navigation controller into a single tab. Then change your app delegate to load the tab bar controller instead of your navigation controller.
If, however, you want to pull out multiple controllers from your existing navigation stack, you might have to do a bit more work - find places where you call pushViewController:animated: to modify your navigation stack, and instead plug those view controller instances into tabs on your tab bar. Here's where it might be worthwhile to start from a fresh tab bar template and copy over existing controllers, depending on the complexity of your code.
Adam,
Not sure how much coding you've done already, but if you're not that familiar with connecting things in IB and setting delegates you might be better off starting over with a TabBarController template.
Here's what you do:
Define new IBOutlet for UITabBarController in AppDelegate and Add TabBarController to IB
Connect UITabBarController to IBOutlet in AppDelegate in IB
Set Delegate for UITabBarController in IB to AppDelegate
Change rootViewController to tabBarController in AppDelegate
Drag All ViewControllers into Tab Bar Controller in IB
Setup other Tabs using NavigationControllers or UIViewControllers depending on your design
Run, Fix, Repeat
Related
I'm making an iphone app out of the utility template in xcode. So in my FlispSideView I have a button that should show a custom image picker. I decided to use this nice one here link.
Now I made some changes cuz I'm not using a navigation controller to load the custom image picker (but rather modally) which is created programmatically inside the .m file. So I made the FlipSideView the delegate for the custom image picker but still lost when I come to loading the view. I created a xib file and tried to connect it to the image picker but that didn't work.
So I wonder what's the best way to do that?
I'm not sure I interpreted your question correctly, but based on the title, I think this might be what you are looking for:
// Initialize your custom controller and set the delegate
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"MyView" bundle:nil];
controller.delegate = self;
// Set the title of your custom controller (optional)
controller.title = NSLocalizedString(#"My View", nil);
// Create a navigation controller with your custom controller as the root view controller
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:controller];
// Present the navigation controller as a modal view controller
[self.navigationController presentModalViewController:navCon animated:YES];
// Release objects you own
[navCon release];
[controller release];
If your image picker is a controller, and all the outlets on your xib are properly connected to it, this should work. You should be able to make your FlipSideView the delegate. Pressing cancel or done in the modal view should call a message in the delegate that says
[self.navigationController dismissModalViewControllerAnimated:YES];
EDIT:
Here is the first line of my example code updated to match the tutorial you are using:
CustomImagePicker *controller = [[CustomImagePicker alloc] init];
The rest is the same. You initialize a navigation controller with controller as the root view controller, then present the navigation controller as a modal view controller.
Creating navigation programatically
Use the code below to navigate programatically. Write the following code in
AppDelegate.m class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
self.nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[_window addSubview:nav.view];
[_window makeKeyAndVisible];
}
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.
I'm having trouble initializing a viewcontroller when the app loads in my app delegate. The viewcontroller loads okay, but it overlaps the tabbar that I have at the bottom. Do I need to create another viewcontroller and have it load into that in the app delegate? I currently have a tabBarController set up in my MainWindow.xib, which contains Navigation controllers and inside those are viewControllers.
Here is my code...
In my didFinishLaunchingWithOptions I have:
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:sub];
[window addSubview:nav.view];
Should it be something like?
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:sub];
[newViewController.view addSubview:nav.view];
Thanks!
If you're trying to use a UITabBarController, each tab has its own root view controller (these can even be UINavigationController objects). Assuming you have a property for self.tabBarController (I think this gets created by default in Xcode if you are doing a tab-bar app), then:
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewContrller" bundle:nil];
nav = [[UINavigationContoller alloc] initWithRootViewController:sub];
[self.tabBarController setViewControllers:[NSArray arrayWithObject:nav] animated:NO];
self.window.rootViewController = self.tabBarController;
// Clean up memory here... only if you don't need references to them
[sub release];
[nav release];
With setViewControllers:animated: you should probably include additional view controllers in the NSArray, otherwise you'll end up with a tab bar only containing one item!
[window addSubview:tabBarController.view]; would be right......
Rather than adding navigation controller using code..... you should add it from Interface Builder.... in interface builder remove the tabBarItem and add a navigationController on it and then set viewController for that navigation controller..........
create an outlet of tabBarController in appDelegate so that you can add it on window. Pleas do not forget to make connection between components in Interface Builder.
Thanks,
my application needs the design similar to sybase ipad app which is here YouTube - Sybase Mobile Sales for SAP CRM on iPad
How can I set a tabbarcontroller as rootview controller of UISplitViewController.
When I try to do this, 8 tabitems are displaying without "More" button. so its overlapping items title. And it will display More button if more than 8 tab items.
As it is using width 320, How to set only 5 tabs visible at a time.
sample
array = [[NSMutableArray alloc] init ];
for(int i=0; i <10; i++){
TestTabController *cc = [[TestTabController alloc]init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:cc] ;
[cc release];
[array addObject:navController];
[alertNavController release];
}
mytabBarController = [[UITabBarController alloc] init];
mytabBarController.viewControllers = array;
splitViewConntroller = [[UISplitViewController alloc] init];
[splitViewConntroller setViewControllers:[NSArray arrayWithObjects:mytabBarController, detailNavigationController, nil]];
How can I set the detailController view as a controller in the tabbarcontrolller(rootController) at runtime -Any easy methods ?-.. We can see when user tap on a cell in the detailController view, it immediatly move to rootController and its detail will show in detailController
Any help/comments/suggestions would be grealy appreciated.
If you create a UISplitViewController based project from the project templates and then open the MainWindow.xib file in interface builder, you can then just drag a tab bar controller component from the Library palette onto the first view controller (the navigation controller) inside the split view controller. Then you should be able to just start adding view controllers to the new tab bar controller. You then just specify the view controllers you want to use for each of the tabs. I've created several projects this way and it works quite well.
Of course, I'm assuming Xcode 3 here. I'm not completely sure if it's the same in Xcode 4 if that's what you're using.
if I'm not mistaken, UITabBarController should always be the root view controller in all cases.
So you're going the opposite way.
I want to customize the look and feel of the tab bar of a UITabBarController. I want to change the colors, the way the icon looks when they are selected, and also, most important of all, I want to reduce the size of the custom toolbar.
My approaches for this and the hurdles in it are:
A) The first solution which came to my mind was to create my own viewController which will act like a UITabBarController with buttons in the bottom and add this viewController to the window. Once when user taps a button at the bottom, swap the view in the viewable area with the new viewController's which corresponds to the button now tapped by user.
The problem with this strategy is: since I swap view's the corresponding viewControllers will not get these messages:
viewWillAppear
viewWillDisappear
viewDidAppear
viewDidDisappear
And all the rotation events
B) I could have used the accepted answer's approach in this thread:
Custom UITabBarController Problems with View Controllers and Views
But my tabBar's height is not the same as the default.
Due to the cited reasons above, I cannot use those approaches.
Having said this, I have no special requirement of More tab. I will be having only 5 tabs which will be displayed by the tab bar and hence the re-ordering of tab bar items is out of scope.
Awaiting suggestions and ideas.
I have never attempted something like this but as I see it, you are supposed to send those messages to your child view controllers manually.
It shouldn't be problem to send -viewWill/Did(Dis)Appear to the right controller at the appropriate moment. This is what UITabBarController does, too.
As for rotation events:
In shouldAutorotateToInterfaceOrientation:, forward this message to your child controllers and set your return value depending on their return values (UITabBarController only returns YES if all its child controllers return YES for the requested orientation).
Forward willRotateToInterfaceOrientation:duration:, didRotateFromInterfaceOrientation: and willAnimateRotationToInterfaceOrientation:duration: to the child controllers (at least to the currently visible one) when you receive them.
If you have set the autoresizing masks of your child controllers' views correctly, they you rotate and resize correctly when the system rotates your custom tab bar controller's view. (At least I think that's how it should work.)
Again, I'm not sure if this will work.
You can implement the following code for the creating the custom tab bar in that use to images using the CGRect make.further code is use for the creating the custom tab bar
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
tabBarController.delegate = self;
tabBarController = [[UITabBarController alloc] init];
mainDashBoard = [[DashBoard alloc] initWithNibName:#"DashBoard" bundle:nil];
mainSearchView = [[SearchView alloc] initWithNibName:#"SearchView" bundle:nil];
mainMoreView = [[MoreView alloc] initWithNibName:#"MoreView" bundle:nil];
UINavigationController *nvCtr0 = [[[UINavigationController alloc] init] autorelease];
UINavigationController *nvCtr1 = [[[UINavigationController alloc] initWithRootViewController:mainDashBoard] autorelease];
UINavigationController *nvCtr2 = [[[UINavigationController alloc] initWithRootViewController:mainSearchView] autorelease];
UINavigationController *nvCtr3 = [[[UINavigationController alloc] initWithRootViewController:mainMoreView] autorelease];
UINavigationController *nvCtr4 = [[[UINavigationController alloc] init] autorelease];//[[[UINavigationController alloc] initWithRootViewController:nil] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:nvCtr0,nvCtr1,nvCtr2,nvCtr3,nvCtr4,nil];
nvCtr0.tabBarItem.enabled = NO;
nvCtr4.tabBarItem.enabled = NO;
[window tabBarController.view];
}