ios 不支持推送导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17757072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pushing a navigation controller is not supported
提问by Jose
In my MainStoryBoard I want to push a viewController to the detailView but I get this error:
在我的 MainStoryBoard 中,我想将 viewController 推送到 detailView,但出现此错误:
NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
NSInvalidArgumentException',原因:'不支持推送导航控制器'
I set the identifier 'JSA' ID for the viewController on the storyboard.
我在情节提要上为 viewController 设置了标识符“JSA”ID。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
SWSJSAViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"JSA"];
[self.navigationController pushViewController:viewController animated:YES];
}
}
回答by Tiago Almeida
Like rmaddysaid in the comments you are trying to push a navigation controller.
就像rmaddy在评论中所说的那样,您正在尝试推送导航控制器。
Navigation controllers should be presented (via presentViewController or they can be added as a childViewController) and ViewControllers should be pushed.
应该呈现导航控制器(通过presentViewController 或者它们可以添加为childViewController)并且应该推送ViewControllers。
回答by Hexfire
When you talk about pushingNavigation Controller, it is most likelythat you want to present it.
当您谈论推送导航控制器时,您很可能想要展示它。
- PresentingUINavigationController
- 呈现UINavigationController
This is the most common way and this is what you want to do in most cases. UINavigationController
cannot be pushed, it can only be presented with a new root View Controller.
这是最常见的方式,也是您在大多数情况下想要执行的操作。UINavigationController
不能推送,只能用新的根视图控制器呈现。
MyViewController* vc = [[MyViewController alloc]
initWithNibName:@"MyController" bundle:nil];
UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentViewController:myNav animated:YES completion:nil];
What you do here, is firstly create a UINavigationController
and then set necessary UIViewController
as its root controller.
你在这里做的是首先创建一个UINavigationController
,然后将必要的设置UIViewController
为它的根控制器。
- PushingUINavigationController
- 推送UINavigationController
If you have a hierarchy of ViewControllers and you need to push view controller that contains navigation controller within, steps are:
如果您有一个 ViewControllers 层次结构并且您需要推送包含导航控制器的视图控制器,步骤是:
1) Push ViewController, containing UINavigationController
.
1) 推送 ViewController,包含UINavigationController
.
To pushUINavigationController
, first create a subclass of UIViewController
, which will be a wrapper-/container- class for your UINavigationController
and its content.
要推送UINavigationController
,首先创建 的子类UIViewController
,这将是您UINavigationController
及其内容的包装器/容器类。
ContainerViewController* vc = [[ContainerViewController alloc] init];
2) Adding UINavigationController as a child view controller
2) 添加 UINavigationController 作为子视图控制器
In viewDidLoad
of your container (that you have just instantiated) simply add something like this:
在viewDidLoad
您的容器(您刚刚实例化)中,只需添加如下内容:
Objective-C
目标-C
UINavigationController* myNav = [[UINavigationController alloc] initWithRootViewController: rootViewController];
[myNav willMoveToParentViewController:self];
myNav.view.frame = self.view.frame; //Set a frame or constraints
[self.view addSubview:myNav.view];
[self addChildViewController: myNav];
[myNav didMoveToParentViewController:self];
Swift 4.2+
斯威夫特 4.2+
let childNavigation = UINavigationController(rootViewController: viewController)
childNavigation.willMove(toParent: self)
addChild(childNavigation)
childNavigation.view.frame = view.frame
view.addSubview(childNavigation.view)
childNavigation.didMove(toParent: self)
What you do here is basically instantiate your navigation controller and add it as a child controller to your wrapper. That's it. You have successfully pushedyour UINavigationController.
您在这里所做的基本上是实例化您的导航控制器并将其作为子控制器添加到您的包装器中。就是这样。您已成功推送您的 UINavigationController。