Search code examples
iphoneiosuitableviewuitabbar

How can i take a UITabbar in a viewController?


If i go straight to the point then my problem is i have an UIViewController with two button.

  1. Button One --(onclick must navigate to)--->UITabbarController with 4 UITabbar item or i can take a UITabbar in my new UIViewController;

  2. Button Two --(OnClick must navigate to)--->UITabbarController where first tabbaritem will contain containing UITableView or or i can take a UITabbar in my new UIViewController where first TabbarItem will contain a UITableView.

How can i make this??? sample code will more helpful neither suggestion. if you need any clarification please do not hesitate to ask.

With Best Regards Emon


Solution

  • First, you need to have a NavigationController if you want another view controller to be pushed on when you click a button. The steps you would need to do are the following

    1. Start with a MasterDetail app on iPhone
    2. In the xib file, put your buttons and wire them up to the correct methods
    3. In the view did appear of the view controller have the following code

      self.tbc1 = [[UITabBarController alloc] init];
      self.tbc2 = [[UITabBarController alloc] init];
      
      
      UIViewController *tbc1vc1 = [[UIViewController alloc] init];
      UIViewController *tbc1vc2 = [[UIViewController alloc] init];
      UIViewController *tbc1vc3 = [[UIViewController alloc] init];
      
      UITableViewController *tbc2vc1 = [[UITableViewController alloc] init];
      UIViewController *tbc2vc2 = [[UIViewController alloc] init];
      UIViewController *tbc2vc3 = [[UIViewController alloc] init];
      
      [self.tbc1 setViewControllers:[NSArray arrayWithObjects:tbc1vc1,tbc1vc2,tbc1vc3, nil]];
      [self.tbc1 setViewControllers:[NSArray arrayWithObjects:tbc2vc1,tbc2vc2,tbc2vc3, nil]];
      
    4. Ofcourse you have to have properties defined for tbc1 adn tbc2 in your view controller.

    5. In the app delegate do the following

          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      
      ViewController *myViewController = [[ViewController alloc] init];
      self.viewController = [[UINavigationController alloc] initWithRootViewController:myViewController];
      self.window.rootViewController = self.viewController;
      [self.window makeKeyAndVisible];
      return YES;
      
    6. Change the UIViewController in the .h file to UINavigationController like

      @property (strong, nonatomic) UINavigationController *viewController;
      
    7. Have the two buttons and wire it to the following methods

      -(IBAction)tbc1Clicked{
      [self.navigationController pushViewController:self.tbc1 animated:YES];
      }
      
      -(IBAction)tbc2Clicked{
          [self.navigationController pushViewController:self.tbc2 animated:YES];
      }