首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查当前viewController或根viewController?

如何检查当前viewController或根viewController?
EN

Stack Overflow用户
提问于 2020-02-13 19:50:33
回答 2查看 174关注 0票数 0

我已经用UITabBarDelegate设置了我的class,并实现了它的method didSelectItem来检测某个tabBar项何时被按下。效果很好。在每个tabBar项中,我有一个containerView,如果用户没有登录,它可以显示“您必须登录”-page,另一个containerView表示嵌入在navigationController中的viewControllers

我想跟踪当前tab项中显示的viewController,和/或该tabroot viewController

我已经尝试了许多不同的方法,但大多数方法都返回nil,或者我无法让它工作。我认为整个container的情况让它更难处理。

它看起来像这样:

代码语言:javascript
复制
@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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-20 04:00:34

很抱歉,没有正确阅读您的问题。这是我建议你做的。

所有这些您有兴趣跟踪的视图控制器:您应该让它们从它们的-viewDidAppear: (或-viewWillAppear:)方法中发送一个自定义通知。然后让您的ApolloTabBarController对象注册该通知。当它收到通知时,您可以存储对视图控制器的引用。该引用将始终指向活动视图控制器。

在您的单个视图控制器中,执行类似以下操作:

代码语言:javascript
复制
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];

    [nc postNotificationName:@"XYZViewControllerDidBecomeActiveNotification"
                      object:self];
}

当然,您可能希望使用某种常量作为通知名称。

在您的ApolloTabBarController类中,注册XYZViewControllerDidBecomeActiveNotification并实现如下内容:

代码语言:javascript
复制
- (void)viewControllerDidBecomeActive:(NSNotification *)notification
{
    self.activeViewController = [notification object];
}

我希望这对你有帮助!

票数 1
EN

Stack Overflow用户

发布于 2020-02-20 00:56:00

在为每个选项卡设置每个视图控制器时,将UITabBarItemtag属性设置为与选项卡栏的viewControllers数组中的视图控制器的索引相对应。

代码语言:javascript
复制
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];

然后,您将能够获取对视图控制器的引用。

代码语言:javascript
复制
// In your example, your ApolloTabBarController acts as its own delegate.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    UIViewController* activeVC = 
        [[self viewControllers] objectAtIndex:[item tag]];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60207156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档