ios 警告:-不鼓励在分离的视图控制器上展示视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19890761/
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
Warning :-Presenting view controllers on detached view controllers is discouraged
提问by Gagan Joshi
In my app, I am using a navigation controller. Later on in some view I am using presentViewController
for showing a zoomed image.
Also I am not using a Storyboard or nib.
在我的应用程序中,我使用了导航控制器。稍后在某些视图中,我presentViewController
用于显示缩放图像。我也没有使用故事板或笔尖。
I am getting this error in iOS 7 only. It works fine in iOS 6 and earlier:
我仅在 iOS 7 中收到此错误。它在 iOS 6 及更早版本中运行良好:
Presenting view controllers on detached view controllers is discouraged
不鼓励在分离的视图控制器上呈现视图控制器
采纳答案by Gagan Joshi
One of the solution to this is if you have childviewcontroller So you simply presentviewcontroller on its parent by given
对此的解决方案之一是,如果您有 childviewcontroller 所以您只需通过给定的方式在其父级上呈现viewcontroller
[self.parentViewController presentViewController:viewController animated:YES completion:nil];
And for dismiss use the same dismissview controller.
对于解雇使用相同的dismissview控制器。
[self dismissViewControllerAnimated:YES completion:nil];
This is perfect solution works for me.
这是对我有用的完美解决方案。
回答by cdescours
To avoid getting the warning in a push navigation, you can directly use :
为了避免在推送导航中收到警告,您可以直接使用:
[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];
And then in your modal view controller, when everything is finished, you can just call :
然后在你的模态视图控制器中,当一切都完成后,你可以调用:
[self dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
回答by Gagan Joshi
The reason of this warning is i was presenting a view controller over a small view that is not full size view. Given below is the image of my project. where on click on four option above. User navigate to different childviewcontroller's view.(it works like tabViewcontroller). But the childviewcontroller contains view of small size. So if we present a view from childviewcontroller it gives this warning.
这个警告的原因是我在一个不是全尺寸视图的小视图上展示了一个视图控制器。下面给出的是我的项目的图像。单击上面的四个选项。用户导航到不同的 childviewcontroller 的视图。(它的工作方式类似于 tabViewcontroller)。但是 childviewcontroller 包含小尺寸的视图。因此,如果我们从 childviewcontroller 呈现一个视图,它会发出此警告。
And to avoid this, you can present a view on childviewcontroller's parent
为了避免这种情况,您可以在 childviewcontroller 的父级上显示一个视图
[self.parentViewController presentViewController:viewController animated:YES completion:nil];
回答by Azaxis
Wait for viewDidAppear()
:
等待viewDidAppear()
:
This error can also arise if you are trying to present view controller before view actually did appear, for example presenting view in viewWillAppear()
or earlier.
Try to present another view after viewDidAppear()
or inside of it.
如果您尝试在视图实际出现之前呈现视图控制器,例如在viewWillAppear()
或更早的时间呈现视图,也会出现此错误。尝试viewDidAppear()
在它之后或内部呈现另一个视图。
回答by Kjuly
In my case, I've a sampleViewController
's view added as a subview, then tries to present a popover from the view of sampleViewController
(here self
instead a UIViewController
instance):
就我而言,我将 asampleViewController
的视图添加为子视图,然后尝试从视图sampleViewController
(此处self
改为UIViewController
实例)呈现弹出窗口:
[self.view addSubview:sampleViewController.view];
The right way should be below:
正确的方法应该在下面:
// make sure the vc has been added as a child view controller as well
[self addChildViewController:sampleViewController];
[self.view addSubview:sampleViewController.view];
[sampleViewController didMoveToParentViewController:self];
B.t.w., this also works for the case that present a popover form a tableview cell, you just need to make sure the tableview controller has been added as child view controller as well.
顺便说一句,这也适用于在 tableview 单元格中显示 popover 的情况,您只需要确保 tableview 控制器也已添加为子视图控制器。
回答by Daniel Ytterbrink
I think that the problem is that you do not have a proper view controller hierarchy. Set the rootviewcontroller of the app and then show new views by pushing or presenting new view controllers on them. Let each view controller manage their views. Only container view controllers, like the tabbarviewcontroller, should ever add other view controllers views to their own views. Read the view controllers programming guide to learn more on how to use view controllers properly. https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/
我认为问题在于您没有适当的视图控制器层次结构。设置应用程序的 rootviewcontroller,然后通过在它们上推送或呈现新的视图控制器来显示新视图。让每个视图控制器管理他们的视图。只有容器视图控制器,如 tabbarviewcontroller,才应该将其他视图控制器视图添加到它们自己的视图中。阅读视图控制器编程指南以了解有关如何正确使用视图控制器的更多信息。https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/
回答by Jeremie
Swift 3
斯威夫特 3
For anyone stumbling on this, here is the swift answer.
对于任何在这方面绊倒的人,这里是快速的答案。
self.parent?.present(viewController, animated: true, completion: nil)
回答by averem
I have almost the same problem. The reason was that I tried to present "some" controller on another and after animation was completed I was setting presented controller as root. After this operation all further controllers presenting bring me to the warning: "Presenting view controllers on detached view controllers is discouraged". And I solve this warning just settings "some" controller as root without any presentation at the begin.
我有几乎同样的问题。原因是我试图在另一个控制器上呈现“一些”控制器,动画完成后我将呈现的控制器设置为 root。在此操作之后,所有其他控制器呈现给我警告:“不鼓励在分离的视图控制器上呈现视图控制器”。我解决了这个警告,只是将“某些”控制器设置为 root,在开始时没有任何演示。
Removed:
删除:
[[self rootController] presentViewController:controller animated:YES completion:^{
[self window].rootViewController = controller;
[[self window] makeKeyAndVisible];}];
Just make as root without any presentation:
只需以 root 身份制作,无需任何演示:
[[self window] setRootViewController:controller];
回答by Tao Fang
Use [self.navigationController presentViewController:xxx animated:YES completion:nil]
in iOS 8.
[self.navigationController presentViewController:xxx animated:YES completion:nil]
在 iOS 8 中使用。
回答by Vlad
Try this code
试试这个代码
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:<your ViewController object>];
[self.view.window.rootViewController presentViewController:navigationController animated:YES completion:nil];