0

I have my app like this : a navigationBar in the app delegate with have a Controller1(UIViewController) like a rootController, in the controller1 i push controller2 ( UIViewController), the controller2 have 3 UINavigationController, and a Custom tabBar, each navigationController have a root controller, and finally i put all the navigationController in the CustomTabBar.

My question is : is this clean( good) to do like this ? If no how i can organize my project ?

MyAppDelegate.h 
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (strong, nonatomic) CustomTabBar *tabBarController;

MyAppDelegate.m {
UIViewController *controller1 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    navigationController = [[UINavigationController alloc] initWithRootViewController:controller1];
 self.window.rootViewController = navigationController;
}

controller1.h
    UIViewController controller2;
    UINavigationController *navigationController2;

    UIViewController controller3;
    UINavigationController *navigationController3;


    UIViewController controller3;
    UINavigationController *navigationController3;

controller1.m 
-(void)viewDidLoad{

viewController1 = [[UIViewController......
navigationController1 = [[UINavigationController alloc] initWithRootViewController:controller1];
....

AppDelegate *apDelegate= [UIApplication sharedApplication].delegate;

    apDelegate.tabBarController = [[CustomTabBar alloc] initWithNibName:nil bundle:nil];
   [apDelegate.tabBarController setViewControllers: [NSArray arrayWithObjects:navigationController1,navigationController2,navigationController3,nil]];
}
samir
  • 4,501
  • 6
  • 49
  • 76

3 Answers3

1

This is an excerpt from the apple documentation:

When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.

From my point of view it's a bit tricky from the start to sort out how to use UITabBarController class, so in this case the better approach is to see some good manual. For me this one helps always when I start to mess around with this UI thing :)

Good luck.

EDIT:
In order to have your tabbar appear only in some concrete views, you have to hide your tabbar from the start of the app, and make it appear only when you really need it. In order to hide it, you can use the method:

[theTabBar setHidden:YES];
makaron
  • 1,585
  • 2
  • 16
  • 30
  • thanks, it means that i can't do sometink like this :[self.view addSubview:apDelegate.tabBarController.view]; – samir Feb 26 '12 at 14:46
  • exactly. Your tabbar should be in app delegate in Window object, if I'm not wrong. Spend these 20 mins to watch a video I gave a link to, and I think you won't regret. – makaron Feb 26 '12 at 14:51
  • but i don't want that my TabBar appear in the first controller, i won't taht it appear when i push my viewContproller1, i will wath the video – samir Feb 26 '12 at 14:59
  • Yes, once I had the same task too. And you **can** do this. You'll just need to hide the tabbar from the start according to the rules of apple. – makaron Feb 26 '12 at 15:17
  • To hide tabbar you can do something like here http://stackoverflow.com/questions/2257178/how-to-hide-tab-bar-controller-programmatically – makaron Feb 26 '12 at 15:23
0

Set the tabBarController as rootViewController of window object:

self.window.rootViewController = tabBarController;

Or you can set tabBarController.view as subview of window object:

[self.window addSubView:tabBarController.view];
minux
  • 2,694
  • 2
  • 18
  • 16
  • thanks for your answer, but i don't want that my tabbar appear in the first view, i want it in the second view, how i can do this ? – samir Feb 26 '12 at 23:28
  • please, take a look at my last comment - I was having the same task, and it's considered to be implemented in the way suggested there. You must remove the tabbar from the screen from the start of the app. This is an approach, chosen by apple - and **not** some ugly way around. – makaron Feb 27 '12 at 10:43
0

if you want add the tabBarController to the secondo view:

[secondViewController.view addSubView:tabBarController.view];

or, for the navigationController

[navigationController1.view addSubView:tabBarController.view];
or
navigationController1.rootViewController = tabBarController;

In other words in the controller1.m you declare a TabBarController and add navController1, navController2 etc.

Then add the tabBarController to controller1 as rootViewController or as subView.

I hope this is what you were looking!

minux
  • 2,694
  • 2
  • 18
  • 16