rickytan / RTRootNavigationController

Implicitly make every view controller has its own navigation bar
MIT License
2.15k stars 379 forks source link

如何在appdelegate中获取当前显示的控制器,用于支付或推送启动返回应用的跳转 #111

Closed jianmu297 closed 7 years ago

jianmu297 commented 7 years ago

如题

rickytan commented 7 years ago

如果你的 rootViewController 是本项目的 VC 的话:

((RTRootNavigationController *)self.window.rootViewController).rt_topViewController
jianmu297 commented 7 years ago

rootvc 是通过initWithRootViewControllerNoWrapping初始化的tabbar,上面的办法貌似不行。。

rickytan commented 7 years ago

tabbar 外面有包一层 navigation 的吧,不然怎么 push

jianmu297 commented 7 years ago
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    EnterPointTabbarController* vc = [StoryBoardManager shareManager].enterPointMoudle.tabBarVC;
    self.window.rootViewController = [[RTRootNavigationController alloc] initWithRootViewControllerNoWrapping:vc];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];`
以上是rootvc的代码,按照demo的例子写的   tabbarvc就一个tabbarviewcontroller 没套nav
rickytan commented 7 years ago

这个不是这么用的。分两种情况来处理:

  1. 你需要 push 后保持 tabBar 可见

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main"
                                                    bundle:nil];
    UITabBarController *tabController = [[RTTabBarController alloc] init];
        tabController.viewControllers = @[[[RTRootNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Root"]],
                                          [[RTRootNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Remove"]],
                                          [[RTRootNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Scroll"]],
                                          [[RTRootNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Table"]]];
        self.window.rootViewController = tabController;
    
    [self.window makeKeyAndVisible];
    }

    这时,root 是 tabbar controller 本身

  2. push 后 tabBar 不可见

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main"
                                                    bundle:nil];
    UITabBarController *tabController = [[RTTabBarController alloc] init];
    
        tabController.viewControllers = @[[[RTContainerNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Root"]],
                                          [[RTContainerNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Remove"]],
                                          [[RTContainerNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Scroll"]],
                                          [[RTContainerNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Table"]],
                                          [[RTContainerNavigationController alloc] initWithRootViewController:[story instantiateViewControllerWithIdentifier:@"Status"]]];
        self.window.rootViewController = [[RTRootNavigationController alloc] initWithRootViewControllerNoWrapping:tabController];
    
    [self.window makeKeyAndVisible];
    }

    这时,tabbar controller 是包在 navigation 中的

jianmu297 commented 7 years ago

恩 我是第二种,只不过代码没有贴出来 这种页面结构下 要取当前显示的vc要怎么写

rickytan commented 7 years ago

建议你用一个单例来解决这种问题,如:[PaymentManager sharedManager],在业务 VC 上设置 delegate,在 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 中用那个 manager 回调到 delegate 上。

不过你一定要拿到当前 VC 的话可以这样:

vc = ((RTRootNavigationController *)self.window.rootViewController).rt_topViewController;
jianmu297 commented 7 years ago

恍然大悟,受教了!