gpt4 book ai didi

ios - 如何从 NSObject 类获取导航 Controller 并在 ios 中推送 View Controller

转载 作者:行者123 更新时间:2023-11-28 18:05:04 24 4
gpt4 key购买 nike

我在一个静态库项目中工作,我想在 iOS 项目的导航 Controller 中推送一个 View Controller 我该怎么做?我是这样做的:

  UIStoryboard * storyboard = [[[UIApplication sharedApplication] keyWindow] rootViewController].storyboard;
UIViewController* pushtoVC = [storyboard instantiateViewControllerWithIdentifier:@"home"];
UINavigationController *navController = [[[[UIApplication sharedApplication] keyWindow]rootViewController] navigationController];
[navController pushViewController:pushtoVC animated:YES];

我也试过下面的代码,但它没有推送 View Controller 。

  UIStoryboard * storyboard = [[[UIApplication sharedApplication] keyWindow] rootViewController].storyboard;
UIViewController* pushtoVC = [storyboard instantiateViewControllerWithIdentifier:@"home"];
UINavigationController *navController = [[[[UIApplication sharedApplication] keyWindow]rootViewController] navigationController];
[navController pushViewController:pushtoVC animated:YES];

但是,如果我以模态方式使用当前 View Controller ,它可以正常工作,但导航 Controller 仅隐藏已加载的 View Controller 。在没有导航 Controller 的情况下以模态方式呈现的代码如下。

 UIStoryboard * storyboard = [[[UIApplication sharedApplication] keyWindow] rootViewController].storyboard;
if ([[storyboard valueForKey:@"identifierToNibNameMap"] objectForKey:@"home"]) {
// the view controller exists, instantiate it here
UIViewController* myVC = [storyboard instantiateViewControllerWithIdentifier:@"home"];
myVC.modalPresentationStyle = UIModalPresentationFullScreen;
[[[[UIApplication sharedApplication]keyWindow] rootViewController] presentViewController:myVC animated:YES completion:nil];

上面的代码以模态方式呈现 View Controller ,但从 View Controller 中删除了导航 Controller ,但我想使用导航 Controller 导航到主视图 Controller 。

最佳答案

目前还不完全清楚您要尝试做什么,因此可以说您应该传递一个 Controller 用于演示,或者将 Controller 传回给知道如何展示它的其他人...

无论如何,我猜你的根 VC 是一个导航 Controller ,所以你应该直接使用它而不是向它询问它的导航 Controller (它是 nil)。

UINavigationController *navController = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
[navController pushViewController:pushtoVC animated:YES];

关于ios - 如何从 NSObject 类获取导航 Controller 并在 ios 中推送 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com