如何从标签栏控制器获取"当前"导航控制器

Zhe*_*hen 18 objective-c uitabbarcontroller uinavigationcontroller ios

是否有一种方法来检索标签栏控制器的当前可见导航控制器?

例如,我的程序中有两个标签栏(每个导航控制器一个),如下所示

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{   
   //Method is called when user clicks on a hyperlink in one of view controllers
    NSDictionary *dict = [self parseQueryString:[url query]];
    NSString *userID = [dict objectForKey:@"id"];
    NSString *navconTitle = [dict objectForKey:@"navcon"];


    //intention is to push a view controller onto the CURRENT navigation stack
    [navcon pushViewController:someViewController animated:YES];

    }
}

return YES;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我如何确定当前的导航控制器,以便我可以将更多的viewcontrollers推到它上面?

Joe*_*Joe 61

使用UITabBarControllers selectedViewController属性.

navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)


小智 6

更新 Starsky 对 iOS 13 的 Swift 答案(“'keyWindow' 在 iOS 13.0 中已弃用”)

guard let tabBarVC = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController as? UITabBarController else { return }
    if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
...
}
Run Code Online (Sandbox Code Playgroud)