如何获取“当前”标签栏控制器的导航控制器

发布于 2024-11-16 10:21:55 字数 663 浏览 1 评论 0原文

有没有一种方法可以检索选项卡栏控制器当前的可见导航控制器?

例如,我的程序中有 2 个选项卡栏(每个选项卡一个导航控制器),如下所示

- (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;
}

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

Is there is a method to retrieve tab bar controller's current visible navigation controller?

For example, I have 2 tabbars in my program (one navigation controller each) as below

- (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;
}

Can anyone advise me how I can determine the current navigation controller so that I can push more viewcontrollers onto it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虚拟世界 2024-11-23 10:21:55

使用 UITabBarController selectedViewController 属性。

navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];

Use the UITabBarControllers selectedViewController property.

navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];
我很坚强 2024-11-23 10:21:55

更新 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 {
...
}

Updating Starsky's Swift answer to iOS 13 ("'keyWindow' was deprecated in 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 {
...
}
与风相奔跑 2024-11-23 10:21:55

我认为 UITabBarController selectedViewController 属性应该是您正在寻找的。

因此,从 UITabBarController 方法:-

 [self.selectedViewController pushViewController:someViewController animated:YES];

I think UITabBarController selectedViewController property should be what you are looking for.

So, from a UITabBarController method :-

 [self.selectedViewController pushViewController:someViewController animated:YES];
生活了然无味 2024-11-23 10:21:55

一个 Swift 版本,以防有人无法阅读 Objective-C,并提供了如何从任何地方找到 tabBar 的附加解决方案。我在处理通知时用它来推送到屏幕。

guard let tabBarVC = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController else { return }

if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
    currentNavController.pushViewController(someVC, animated: true)
}

A Swift version, in case someone can't read Objective-C, with an additional solution for how to find the tabBar from anywhere. I use this to push to a screen when processing a notification.

guard let tabBarVC = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController else { return }

if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
    currentNavController.pushViewController(someVC, animated: true)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文