Programmatically pressing a UITabBar button in Xcode

前端 未结 6 1256
无人及你
无人及你 2020-12-17 17:34

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod a

相关标签:
6条回答
  • 2020-12-17 18:02

    alternatively...

    [self.parentViewController.tabBarController setSelectedIndex:3];
    
    0 讨论(0)
  • 2020-12-17 18:03

    I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.

    Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:

    FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
    [self.selectedViewController.view removeFromSuperview]
    [self.view insertSubview:firstVC.view belowSubview:self.tabBar];
    [self.tabBar setSelectedItem:self.firstTabBarItem];
    self.selectedViewController = firstVC;
    

    You might have similar code already inside your didSelectedItem..

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
       if (item == self.firstTabBarItem) 
          // Right here
        }
        else if ...
    }
    
    0 讨论(0)
  • 2020-12-17 18:04
    [myTabBarController setSelectedIndex:index]
    

    EDIT: Answering the part 2 question from the comment:

    You can define a method in AppDelegate for switching to a different tab.

    And you can get hold of appdelegate from anywhere and send a message.. something like:

     MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
     [appDelegate SwitchToTab:index]
    
    0 讨论(0)
  • 2020-12-17 18:04

    I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.

    Anyhow, I hope this helps someone:

    I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:

    AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];
    

    When I should have been doing this:

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

    Newbie mistake.

    0 讨论(0)
  • 2020-12-17 18:05

    For this, You just need to take UITabBar controller -

    .h File -

    UITabBarController *_pTabBarController;
    @property (nonatomic, retain) IBOutlet  UITabBarController *_pTabBarController;
    
    .m File -
    // synthesize it 
    @synthesize  _pTabBarController;    
    
    At initial load 
    
    // You can  write one function to add tabBar - 
    
    // As you have already mentioned you have created an array , if not 
    
    _pTabBarController = [[UITabBarController alloc] init];
        NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
        UINavigationController *theNavigationController;
    
        _pController = [[Controller alloc] initStart];  
        _pController.tabBarItem.tag = 1;
        _pController.title = @"Baranches";
        theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
        theNavigationController.tabBarItem.tag = 1;
        theNavigationController.tabBarItem.image = [UIImage imageNamed:@"icon_branches.png"];
        [localViewControllersArray addObject:theNavigationController];
        [theNavigationController release];
    

    than you can set index as per your needs

    self._pTabBarController.selectedIndex = 0; // as per your requirements
    
    0 讨论(0)
  • 2020-12-17 18:16
    [self.parentViewController.tabBarController setSelectedIndex:3];
    

    Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.

    Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:

    UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
    [MyTabController setSelectedIndex:1];
    

    Thank you for this post/answers it helped out a lot in my project.

    0 讨论(0)
提交回复
热议问题