iOS:在'自动TabBar切换'方法

时间:2012-08-21 18:41:45

标签: ios

我正在尝试以编程方式切换标签,并确保在交换机上相应的导航控制器弹出到根视图控制器。是否有手动或以编程方式切换标签栏时调用的方法?

注意: -(void)tabBarController:didSelectViewController:仅在手动切换标签栏时调用

2 个答案:

答案 0 :(得分:1)

您也可以尝试以编程方式调用didSelectViewController。在这里查看babbidi的答案:

How to trigger method "tabBarController:didSelectViewController:" programmatically?

答案 1 :(得分:0)

FWIW,另一种方法是在selectedViewController上使用观察者。

// Add Observer
// Note: tabBarController.selectedIndex is not observed as it does not call observeValueForKeyPath on manual switch
[self.tabBarController addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:@"changedTabbarIndex"];

// Method for Handling Observations
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSString *action = (__bridge NSString*)context;
    if([action isEqualToString:@"changedTabbarIndex"])
    {
        // Stuff to do on selected Tab changed
    }
}

// Change selectedViewController
[self.tabBarController setSelectedViewController:[[self.tabBarController viewControllers] objectAtIndex:kSomeTab]];

更多信息: I didn't get any notifications when I touched on tabbar items