0

I have added tabbarcontroller programmatically and then added two viewcontrollers to it.The code for the same is:

ExampleViewcontroller.m

self.tabController = [[UITabBarController alloc] init];

viewController1 =[[invoiceviewcontrolleralloc]
initWithNibName:@"invoiceviewcontroller" bundle:nil];

viewController1.title = @"Unpaid Invoice";

viewController2=[[remittanceviewcontrolleralloc]
initWithNibName:@"remittanceviewcontroller" bundle:nil];

tabController.view.frame = CGRectMake(0, 0, 320, 460);

self.tabController.viewControllers = [NSArray arrayWithObjects:
viewController1,viewController2,nil];

[self.view addSubview:tabController.view]; 
tabController.delegate=self;

self.view bringSubviewToFront:tabController.view];

and the method of tabbarcontroller is as follows:-

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:
(UIViewController *)viewController
{

if(tabController.selectedIndex==0)
{
[viewController2.view removeFromSuperview];

[self.view addSubview:viewController1.view];
}

else  if(tabController.selectedIndex==1)
{    
[viewController1.view removeFromSuperview];

[self.view addSubview:viewController2.view];
}

[self.view bringSubviewToFront:tabController.view];
}

The code runs fine but the moment I click on the second tab the viewcontroller attach to loses interactivity i.e the functionality in it does not respond to click.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
rehan
  • 141
  • 1
  • 2
  • 9

1 Answers1

0

UITabBarController is meant to sit at the top of your UIVIewController hierarchy, or directly presented modally. You can't put its view inside of another one of your UIViewControllers' views and expect it to work correctly. In fact, whenever you find yourself manually adding the views of one view controller to another, you're probably doing it wrong. The view controllers will handle doing that for you if you're using them appropriately.

Please read "Creating a Tab Bar Interface" here and pay particular attention to the bullet points: http://developer.apple.com/library/ios/ipad/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/TabBarControllers.html#//apple_ref/doc/uid/TP40011313-CH3

leftspin
  • 2,468
  • 1
  • 25
  • 40