如何从 NSObject 类获取导航控制器并在 ios 中推送视图控制器 [英] how to get navigation controller from NSObject class and push a view controller in ios

查看:72
本文介绍了如何从 NSObject 类获取导航控制器并在 ios 中推送视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working in a static library project and I want to push a view controller in navigation controller of iOS project How can I do it ? I am doing it as follows:

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

我也尝试了以下代码,但它没有推送视图控制器.

I have also tried the following code but its is not pushing 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];

但是,如果我以模态方式使用当前视图控制器,它可以正常工作,但导航控制器隐藏仅加载了视图控制器.在没有导航控制器的情况下以模态呈现的代码如下.

But if I am using present view controller modally it is working but navigation controller hides only View controller was loaded. the code which is working to present modally without navigation controller is as follows.

 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];

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

the above code is presenting view controller modally but navigation controller removed from view controller but I want to navigate to home view controller with navigation controller.

推荐答案

并不完全清楚你想要做什么,所以可以说你应该传递一个控制器来用于演示或将一个控制器传递回其他知道如何呈现它的东西...

It isn't entirely clear exactly what you're trying to do, so arguably you should be passing a controller to use for presentation or passing a controller back to something else who knows how to present it...

无论如何,我猜你的根 VC 是一个导航控制器,所以你应该直接使用它,而不是要求它提供它的导航控制器(它是零).

Anyway, I guess your root VC is a navigation controller so you should be using that directly instead of asking it for its navigation controller (which is nil).

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

这篇关于如何从 NSObject 类获取导航控制器并在 ios 中推送视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆