Search code examples
iosobjective-cuitabbarcontrolleruitabbaritemtabbarcontroller

Why i need to tap tabBarViewController item Twice to navigate to different View Controller - iOS


I Am Working on an Application where there is a TabBar present on My Menu Screen. It has 5 view Controllers (5 Tabs). I am assigning 2 view controllers at the index 3 and 4 respectively, so that if i click on any of the two, they will navigate to their desired view Controllers.

Every thing works well if i select an item in tab bar for the first time. But, when i come back to that view controller and try to press it again , then the item gets selected for first click, but does not navigate to the view controller.I have to click it again to perform that same action. i.e. I have to click it twice for Navigation.

Here i have posted some code regarding my question.

//...In my appdelegate class
CalenderScreen *calender=[[CalenderScreen alloc]initWithNibName:@"CalenderScreen" bundle:nil];
    NewsScreen *news=[[NewsScreen alloc]initWithNibName:@"NewsScreen" bundle:nil ];

    UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:menu];
    UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:emergency];
    UINavigationController *nav4=[[UINavigationController alloc] initWithRootViewController:help];
    UINavigationController *nav3=[[UINavigationController alloc]initWithRootViewController:calender];
    UINavigationController *nav5 =[[UINavigationController alloc]initWithRootViewController:news];

//tab bar initialization
tab=[[UITabBarController alloc]init];
    tab.viewControllers=[[NSArray alloc]initWithObjects:nav1,nav2,nav4,nav5,nav3 ,nil];
    UITabBarItem *tabItem = [[[tab tabBar] items] objectAtIndex:0];
    [tabItem setTitle:@"Menu"];
    tabItem.image=[UIImage imageNamed:@"Menu.png"];
    UITabBarItem *tabItem1 = [[[tab tabBar] items] objectAtIndex:1];
    [tabItem1 setTitle:@"Emergency"];
    tabItem1.image=[UIImage imageNamed:@"Emergency.png"];
    UITabBarItem *tabItem2 = [[[tab tabBar] items] objectAtIndex:2];
    [tabItem2 setTitle:@"HelpLine"];
    tabItem2.image=[UIImage imageNamed:@"helpline.png"];
    UITabBarItem *tabItem3 = [[[tab tabBar] items] objectAtIndex:3];
    [tabItem3 setTitle:@"News"];
    tabItem3.image=[UIImage imageNamed:@"News.png"];
    UITabBarItem *tabItem4 = [[[tab tabBar] items] objectAtIndex:4];
    [tabItem4 setTitle:@"Raise Ticket"];
    tabItem4.image=[UIImage imageNamed:@"ticket.png"];

     self.window.rootViewController=tab;

    [self.window makeKeyAndVisible];

And in my view controller class, I am having below code

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NewsScreen *news=[[NewsScreen alloc] initWithNibName:@"NewsScreen" bundle:nil];

if (tabBarController.selectedIndex==3)
{
    NSUInteger index=3; //assign value here
    UINavigationController *nv = [[tabBarController viewControllers] objectAtIndex:index];//index of your NewsScreen controller
    [tabBarController setSelectedIndex:3];

    NSArray *array =[nv viewControllers];
    for (news in array)
    {
        if ([news isKindOfClass:[NewsScreen class]])
        {
            news.loginbacklbl.hidden=YES;
        }
    }
}

My worry is, when i click on NewsScreen for the first time, it works fine. But when i come back and try to click the NewScreen item on tabBar, it requires two clicks for me two get the event done. Can anybody help me why this happens, and what should be done to avoid this issue. Please help me out. Any help is appreciated. Thank you.


Solution

  • Single click is used to activate your current tab & double click to go t root... Tell client this is default behavior.

    If you still want to override, you have to make custom tab bar.