why does selecting a tabbarController index programatically doesnt call delegate method

回眸只為那壹抹淺笑 提交于 2019-12-22 06:36:01

问题


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?


回答1:


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];
}



回答2:


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];


来源:https://stackoverflow.com/questions/15278537/why-does-selecting-a-tabbarcontroller-index-programatically-doesnt-call-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!