0
votes

I am new to iOS as my project depends on both navigation and the UITabBar controller.I have done the following steps
UITabBar contains 4 buttons and navigation bar contains 2 buttons i.e is common to all the screens 1)first i have taken the UITabBar controller and added four buttons to it 2)For each button to the UITabBar i have added the navigation controller When i click the tabbar buttons all the views are showing fine and coming to UITabBar bar buttons I am facing the problem as below

suppose I am in UITabBar screen "A" and i clicked the navigation bar button i got the navigation screen ex as "Navscreen"that means now "A" contains "Navscreen" when i clicked tabbar button "B" and came back to UITabBar button "A" still its showing the "Navscreen"

To avoid such cases in the "Navscreen" view controller i have added the code as below in the view will appear

   -(void)tabBarController:(UITabBarController *)tabBarController   
   didSelectViewController:(UIViewController *)viewController
 {
  NSUInteger indexOfTab = [tabBarController.viewControllers 
  indexOfObject:viewController];
  NSLog(@"Tab index = %u (%u)", indexOfTab);

  if (indexOfTab==0)
  {
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"  
 bundle:nil];
    FirstViewController *firstview =
    (FirstViewController *) [storyboard   
   instantiateViewControllerWithIdentifier:@"home"];

    [self.navigationController pushViewController:firstview   
   animated:YES];

  }
   else if (indexOfTab==1)
 {
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" 
  bundle:nil];
    secondviewcontroller *secondview =
    (secondviewcontroller *) [storyboard 
     instantiateViewControllerWithIdentifier:@"Medremainder"];
  [self.navigationController pushViewController:secondview animated:NO];

   }
  if (indexOfTab==2)
    {

    UIStoryboard *storyboard = [UIStoryboard   
    storyboardWithName:@"Main" bundle:nil];
    mymedView *mymed_view =
    (mymedView *) [storyboard 
 instantiateViewControllerWithIdentifier:@"mymed"];
    [self.navigationController pushViewController:mymed_view   
  animated:NO];
        }

 if  (indexOfTab==3)
{

    UIStoryboard *storyboard = [UIStoryboard  
   storyboardWithName:@"Main" bundle:nil];
    Event_view *event_view =
    (Event_view *) [storyboard 
   instantiateViewControllerWithIdentifier:@"Event_view"];
    [self.navigationController pushViewController:event_view  
   animated:NO];

    }
}

But in this case when click "Navscreen" its working and when I click the UITabBar "B" iam able to get the UITabBar screen.If i click again UITabBar "A" i can see "A" and when i click the UITabBar button "B" app is crashing.AS i am new to ios please help me with the possible solution if i make any thing wrong please suggest with the correct solutions.

1
Your description of problem is not very clean, but i suspect you are trying to use TabBar and Navigation controllers in wrong way. Check developer.apple.com/library/ios/documentation/WindowsViews/…Jakub Vano
yes iam using both navigation and tabbar controllers . i need to get the tabscrees when i clicked the tabbar buttons is clicked .But some of the screens are showing the previouly used navigationbar screens that s my problemIOS dev

1 Answers

0
votes

In your didSelectViewController:, you seem to be pushing new controllers onto self.navigationController multiple times? Each time you select a tab this code will push another controller loaded from storyboard on top of what is already there. This is probably what is messing up your navigation items.

Should you not just be allocating separate navigation controllers once to each tab so this is all automatic?

In storyboard/code allocate separate navigation controllers for each tab. In each navigation controller, create it with a root controller based on what you are loading from storyboard for each tab. You only need to create this once somewhere in your main controller. Then allocate the navigation controllers to separate tabs in storyboard or in code set the array of controllers to be an array of your created navigation controllers.

It should then all be automatic without you needing to implement didSelectViewController: at all since each tab will simply cause the appropriate navigation controller to be presented and its associated navigation bar.