3

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.

11
  • sorry if i understood wrong... when you navigate to your tab bar controller you want a complete look of tabbarcontroller or it doesnt matter if the navigation bar at the top exist or not? Commented Aug 22, 2012 at 4:28
  • are you required to support iOS4 ? if you are okay supporting only iOS 5 and beyond, i could post a test .storyboard file i created in a couple of minutes that is set up exactly as your bounty request describes (with the exception of the "Other View Controllers" between the plain rootViewController UIViewController and the UITabBarController). Commented Aug 22, 2012 at 16:03
  • @aj I actually want my navigation bar to be on top. Commented Aug 22, 2012 at 23:28
  • @john.k.doe I am only supporting iOS 5+ but my app is not using storyboard. Commented Aug 22, 2012 at 23:28
  • @Darksky, ok. if you don't get the answers you're interested in for your bounty in the next few days and are interested in inter-mixing them (and it is not so hard to inter-mix storyboard with .xib), i can present the XML as an answer, and you can import it and play with it if you want. Commented Aug 23, 2012 at 0:06

7 Answers 7

14
+50

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];
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

3 Comments

Hi - I don't want my tab bar to be in the AppDelegate. My app delegate goes to some other view controller, which has a navigation controller. From that view controller, I have a button which when clicked, I want to display a tab bar controller. How can that be done? Why are all tab bars assumed to be universal on the whole app? Is what I'm doing bad practice?
AppDelegate is origin of viewController...in AppDelegate put both navController and tabBarConroller...and make navConroller(which goes to some view) as rootController....and at the time of button click just remove navController and add tabBar COntroller...
AppDelegate will initiate a UITabBarController for the whole app. My question clearly states this is not what I want. I will check your other answer.
0

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.

4 Comments

This doesn't work. The YourView needs a UIView to open. In the xib, you can either have a UITabBarController or a UIView. If a UIView is there, then you the tab bar won't show. If a tab bar is there, it will crash since no view will exist in the view controller.
I created a new nib, deleted the view and added a UITabBarController. I connected it to the property and used the above code. I am getting a SIGTRAP.
which class are you making to implement to add tabBar....UIViewController and UITabBarController...
You dont add the tabBarController as the view, the tabBarController manages other view controllers. @Rajneesh071 answered correctly (albeit a little tersely).
0

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.

1 Comment

You mean a custom UITabBarController? Any pointers to what you did exactly?
0

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.

Comments

0

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
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.