why does selecting a tabbarController index progra

2019-05-01 21:14发布

when we touch the tabbaritem of the tabbarcontroller the delegate methods are called:

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

but when try to do the same thing programmatically, i.e.

[self.tabbarController setSelectedIndex:selectedIndexNo];

or

[self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers objectAtIndex:0]];

the delegate methods are not called. What is the reason for that?

2条回答
够拽才男人
2楼-- · 2019-05-01 21:24

When you are setting them yourself via code, than you are aware that this is the time when the delegate method will be called. so whatever you wish to do you can do it at the time of setting the index programmatically. Say you want to call a method aMethod on tabbardelegate being called. you can call the method as soon as you set the index.

[self.tabbarController setSelectedIndex:selectedIndexNo];
[self aMethod];
查看更多
闹够了就滚
3楼-- · 2019-05-01 21:37

override UITabBarController setSelectedIndex:

-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
    //must call super function. 
    [super setSelectedIndex:selectedIndex];

    [self myMethod];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [self myMethod];
}
查看更多
登录 后发表回答