darksky
darksky

Reputation: 21019

Adding UITabBarController to a UIViewController

My app currently has a UINavigationController and I'd like to push a UITabBarController at some point when a button is clicked. I am trying to create it on Interface Builder (as opposed to programatically).

All online tutorials show how to create a tab bar based app, which involves dragging a UITabBarController into the MainWindow.xib which is obviously not what I want.

What I did was create a UIViewController, and its nib file, dragged a UITabBarController. Now pushing that UIViewController to the navigation controller will show an empty view (its empty view). Removing the view in the view controller will crash the app. How can I tell the UIViewController to load a UITabBarController instead of its own view?

For those down-voting me: it would be decent to at least provide a comment. The question is not a poor question. The questions is asking for suggestions for how to use a UITabBarController in an unorthodox way. I tried most of the suggestions and they do not work. If you are down-voting, at least write a comment.

Upvotes: 3

Views: 16731

Answers (7)

08442
08442

Reputation: 521

take a uiview of tab bar controller means create an interface builder with tabs and add that tab bar uivew in your classes where ever u wanted the tab bar

for example take a tab bar uiview of 3 tabs in that uiview take the three buttons in the interface builder

for every navigation of that classu should add this uiview class

-(IBAction)firt_button_pressed:(id)sender
{

}

-(IBAction)second_button_pressed:(id)sender
{

}

Upvotes: 0

darksky
darksky

Reputation: 21019

The problem was, as I believe I've mentioned somewhere, is that the XIB does not have a UIView connected to it. When the UIView is deleted in a XIB file and a UITabBarController is added, the view property of the XIB has to be connected to the UITabBarController's view. I connected it and it worked. That was the reason why I was getting a SIGTRAP.

Upvotes: 0

David Evans
David Evans

Reputation: 213

I remember doing something similar to this...

I had to create a custom UITableViewController to do this, if you are going to use UINavigationController to 'push' to it.

Doing it only in interface builder may be a bit tricky, it's been a while since I've been at it, I do recall it was a bit of a nightmare to get going correctly.

Upvotes: 0

Neo
Neo

Reputation: 2807

You can see this this may help you

Since this is how you want your app to be: - Navigation Controller - Root View Controller - Other View Controllers - Tab Bar Controller - First VC under tab - Second VC under tab - Third VC under tab - more view controllers

in your view controller where you want to pushViewController to UITabBarController use this

//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];

//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];

//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];

Upvotes: 14

jhoanna
jhoanna

Reputation: 1857

This tutorial might help. It comes with an example code.

Upvotes: 1

Rajneesh071
Rajneesh071

Reputation: 31073

In YourView controller make IBOutlet of tabBarController

in .h file

#import <UIKit/UIKit.h>

@interface YourView : UIViewController
{
    IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
@end  

and in .m file

#import "YourView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation YourView

-(IBAction)loadTabBar:(id)sender
{
    FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
    [self.navigationController pushViewController:tabBarController animated:YES];
}
@end  

The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController.

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31073

Hi just make both nav controller and tabBar Controller in app delegate. Initially add navController to your root view..

[self.window addSubview:navigationController.view];  

and whenever you want to add tab bar then remove navController and add tabBarController.

-(void)addTabBarController
{
    AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];

    [[[appdelegte navigationController] view]removeFromSuperview];

    [[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];

    [[appdelegte tabcontroller]setSelectedIndex:0];
}  

If you get any problem then ask me again..

Upvotes: 0

Related Questions