0
votes

I am trying to do the following:

  1. Show a launch page in the app.
  2. Try to log in user by pinging my website. If user is logged in, transition to a tab controller with 3 tabs and navigation controller for each tab.
  3. If user is not logged in, transition to tab controller with 3 tabs but each tab brings the user to different pages (login, register, learn more)

How can I add a tab controller in the view controller and not in the app delegate?

I was going to do the following

in the app delegate set the rootController to be viewcontroller1

UIViewController * viewController1;
viewController1 = [[HTLoginViewController alloc] initWithNibName:@"HTLoginViewController_iPhone" bundle:nil];
self.window.rootViewController = viewController1;

Then in viewcontroller1 call the setTab function below

-(void)setTab { self.tabBarController = [[UITabBarController alloc] init];

UIViewController * viewController1, *viewController2, *viewController3;

viewController1 = [[HTMyJobsViewController alloc] initWithNibName:@"HTMyJobsViewController" bundle:nil];
viewController2 = [[MTViewController alloc] initWithNibName:@"MTViewController" bundle:nil];
viewController3 = [[HTInviteController alloc] initWithNibName:@"HTInviteController" bundle:nil];


viewController1.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];


// account view
UINavigationController * firstNavController = [[UINavigationController alloc]initWithRootViewController: viewController1];
viewController2.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];

// book view
UINavigationController * secondNavController = [[UINavigationController alloc]initWithRootViewController: viewController2];
viewController3.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];

// invite view
UINavigationController * thirdNavController = [[UINavigationController alloc]initWithRootViewController: viewController3];


CATransition *transition = [CATransition animation];
transition.duration = 0.7f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[firstNavController.view.layer addAnimation: transition forKey:nil];
[secondNavController.view.layer addAnimation: transition forKey:nil];
[thirdNavController.view.layer addAnimation: transition forKey:nil];

self.tabBarController.viewControllers = [[NSArray alloc] initWithObjects: firstNavController, secondNavController, thirdNavController,  nil];
self.tabBarController.delegate = self;
self.tabBarController.selectedIndex = 1;

HTAppDelegate*appDelegate = (HTAppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.serivceImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, appDelegate.window.frame.size.height - 66, 108, 58)];
appDelegate.serivceImg.image=[UIImage imageNamed: @"tabi_01.png"];

appDelegate.contactImg=[[UIImageView alloc]initWithFrame:CGRectMake(108,  appDelegate.window.frame.size.height - 66, 107, 58)];
appDelegate.contactImg.image=[UIImage imageNamed: @"tabi_02.png"];

appDelegate.bookingImg=[[UIImageView alloc]initWithFrame:CGRectMake(215,  appDelegate.window.frame.size.height - 66, 105, 58)];

appDelegate.bookingImg.image=[UIImage imageNamed: @"tabi_03.png"];
[ self.tabBarController.view addSubview: appDelegate.serivceImg];
[ self.tabBarController.view addSubview: appDelegate.contactImg];
[ self.tabBarController.view addSubview: appDelegate.bookingImg];


self.tabBarController.tabBar.frame = CGRectMake(0,  appDelegate.window.frame.size.height - 45, 320, 45);

[self.view addSubview: self.tabBarController.view];

What's the best way to get this done?

1
Are you using storyboards? This situation is pretty much a poster child for when to use it.GeneralMike
I want to do this programmmatically.Kermit the Frog
In that case, I'm afraid I can't really help. I don't do any of my view controller stuff programmatically. Maybe the Apple docs will have something to help, maybe not.GeneralMike

1 Answers

0
votes

I would try this with a different structure. First, create the tab bar controller and its three content controllers in the app delegate, and make it the root view controller of the window. In the controller for the first tab, present your launch page modally in the viewDidAppearAnimated: method -- this will make that view the first one to be seen (if you have animate set to NO). If the login succeeds, just dismiss the modal view controller, and you're ready to go. If the login fails, create your 3 new content controllers, set those to the viewControllers property of the tab bar controller, and then dismiss the modal. This way, you just need one tab bar controller, and it's the root view controller, which is the more usual setup.