我已经用UITabBarDelegate设置了我的class,并实现了它的method didSelectItem来检测某个tabBar项何时被按下。效果很好。在每个tabBar项中,我有一个containerView,如果用户没有登录,它可以显示“您必须登录”-page,另一个containerView表示嵌入在navigationController中的viewControllers。
我想跟踪当前tab项中显示的viewController,和/或该tab的root viewController。
我已经尝试了许多不同的方法,但大多数方法都返回nil,或者我无法让它工作。我认为整个container的情况让它更难处理。
它看起来像这样:
@interface MyTabBarController () <UITabBarDelegate>
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
switch (indexOfTab) {
case 0: {
NSLog(@"?PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
case 1: {
NSLog(@"?PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
case 2: {
NSLog(@"?PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
//These return nil
NSLog(@"?AAAAAA %@", ((UINavigationController*)_appD.window.rootViewController).visibleViewController);
NSLog(@"?AAAAAA %@", ((UITabBarController*)_appD.window.rootViewController).selectedViewController);
NSLog(@"?AAAAAA %@", self.navigationController.topViewController);
NSLog(@"?AAAAAA %@", self.navigationController.visibleViewController);
//This returns with a value, but can't get it to work with conditionals, that is, when I'm in root, the else is triggered
NSLog(@"?AAAAAA %@", self.tabBar.window.rootViewController);
if(!self.tabBar.window.rootViewController) {
NSLog(@"???THIS IS NOT ROOT???");
}else {
NSLog(@"???this is ROOT???");
}
// This returns nil
((UINavigationController*)_appD.window.rootViewController).visibleViewController;
((UITabBarController*)_appD.window.rootViewController).selectedViewController;
//Doesn't work
if([self.navigationController.viewControllers[0] isKindOfClass:[ExperiencesListViewController class]]) {
NSLog(@"?IS KIND OF CLASS LIST");
}
if([self.navigationController.viewControllers[0].childViewControllers isKindOfClass:[ExperiencesContainerViewController class]]) {
NSLog(@"?IS KIND OF CLASS CONTAINER");
}
break;
}
case 3: {
NSLog(@"?PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
case 4: {
NSLog(@"?PRESSIIING %lu", (unsigned long)[[tabBar items] indexOfObject:item]);
break;
}
default:
break;
}
}
So, what else can I try? Seems like I have to use `self.tabBar.window.rootViewController` in some way, no?
***EDIT***
Oh, and I have tried the `tabBarController` delegate but that doesn't trigger. Also, the `tabBar` is constructed programmatically if that helps.发布于 2020-02-20 04:00:34
很抱歉,没有正确阅读您的问题。这是我建议你做的。
所有这些您有兴趣跟踪的视图控制器:您应该让它们从它们的-viewDidAppear: (或-viewWillAppear:)方法中发送一个自定义通知。然后让您的ApolloTabBarController对象注册该通知。当它收到通知时,您可以存储对视图控制器的引用。该引用将始终指向活动视图控制器。
在您的单个视图控制器中,执行类似以下操作:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"XYZViewControllerDidBecomeActiveNotification"
object:self];
}当然,您可能希望使用某种常量作为通知名称。
在您的ApolloTabBarController类中,注册XYZViewControllerDidBecomeActiveNotification并实现如下内容:
- (void)viewControllerDidBecomeActive:(NSNotification *)notification
{
self.activeViewController = [notification object];
}我希望这对你有帮助!
发布于 2020-02-20 00:56:00
在为每个选项卡设置每个视图控制器时,将UITabBarItem的tag属性设置为与选项卡栏的viewControllers数组中的视图控制器的索引相对应。
UIViewController* myFirstVC = [[UIViewController alloc] init];
UIViewController* mySecondVC = [[UIViewController alloc] init];
// "self" is your ApolloTabBarController.
[self setViewControllers:@[myFirstVC, mySecondVC]];
myFirstVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:@"First" image:nil tag:0];
mySecondVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:1];然后,您将能够获取对视图控制器的引用。
// In your example, your ApolloTabBarController acts as its own delegate.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
UIViewController* activeVC =
[[self viewControllers] objectAtIndex:[item tag]];
}https://stackoverflow.com/questions/60207156
复制相似问题