通过App Delegate的UITabBarController检测何时按下tabBar项 [英] detect when a tabBar item is pressed via UITabBarController from App Delegate
问题描述
我知道这个问题已经问过几次了,但是当我在AppDelegate
类中放置UITabBarController
并且将viewControllers仅设置为
I know this question has been asked a few times, but I am still stuck on the case when I have my UITabBarController
in my AppDelegate
class and viewControllers are set there only as
self.tabBarController.viewControllers =
@[aboutUsNavController,myProfileNavController,
projectsListNavController, feedsNavController,homeViewController];
现在我要执行的任务是在TabBar项目feedsNavController
不在 viewWillAppear
中点击执行任务(因为它正在通过navigationController
推动detailView
).
Now what I want is to perform a task at the tap of TabBar item feedsNavController
not in viewWillAppear
(because it is pushing a detailView
via navigationController
).
我已经在同一类中设置了委托以执行UITabBarController
委托方法:
I have set delegate in the same class to perform a UITabBarController
delegate method:
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.tabBarController.delegate = self;
但是
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
仅在我切换TabBar项时起作用,但不是第一次?
But
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
only works when I switch the TabBar Item, but not at the first time?
有什么方法可以代替UITabBarDelegate
?
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
以便在按下tabBar item
时获得参考?
in order to get the reference when tabBar item
is pressed?
否则将不胜感激.
推荐答案
找出解决方法:
实际上,UITabBarControllerDelegate
方法仅在加载该特定选项卡后才被调用.
Actually UITabBarControllerDelegate
method only gets called once that particular tab it loaded.
因此,我第一次在viewDidLoad
中执行了任务[self getFeedsFromServer];
,然后在
Therefore, I performed task [self getFeedsFromServer];
in viewDidLoad
(for first time), and then again in
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"Selected INDEX OF TAB-BAR ==> %i", tabBarController.selectedIndex);
if (tabBarController.selectedIndex == 3) {
[self getFeedsFromServer];
}
}
我想要的作品!
这篇关于通过App Delegate的UITabBarController检测何时按下tabBar项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!