cde*_*urs 201
为避免在推送导航中收到警告,您可以直接使用:
[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
然后在你的模态视图控制器中,当一切都完成后,你可以调用:
[self dismissViewControllerAnimated:YES completion:nil];
Gag*_*shi 61
这个警告的原因是我在一个不是全尺寸视图的小视图上呈现了一个视图控制器.以下是我的项目的图像.点击上面的四个选项.用户导航到不同的childviewcontroller的视图.(它的工作方式类似于tabViewcontroller).但是childviewcontroller包含小尺寸的视图.因此,如果我们从childviewcontroller提供一个视图,它会发出此警告.
为避免这种情况,您可以在childviewcontroller的父级上显示视图
[self.parentViewController presentViewController:viewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
小智 58
等待viewDidAppear()
:
如果您尝试在视图实际显示之前呈现视图控制器,例如在其中viewWillAppear()
或之前呈现视图,也会出现此错误.尝试viewDidAppear()
在其之后或之内呈现另一个视图.
Kju*_*uly 21
在我的情况下,我将一个sampleViewController
视图添加为子视图,然后尝试从sampleViewController
(这里self
改为UIViewController
实例)的视图中呈现一个弹出窗口:
[self.view addSubview:sampleViewController.view];
Run Code Online (Sandbox Code Playgroud)
正确的方法应该是:
// 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];
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这也适用于弹出窗体单元格的情况,你只需要确保将tableview控制器添加为子视图控制器.
Dan*_*ink 16
我认为问题是你没有合适的视图控制器层次结构.设置应用程序的rootviewcontroller,然后通过在其上推送或显示新视图控制器来显示新视图.让每个视图控制器管理他们的视图.只有容器视图控制器(如tabbarviewcontroller)才能将其他视图控制器视图添加到自己的视图中.阅读视图控制器编程指南,了解有关如何正确使用视图控制器的更多信息.https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/
Jer*_*mie 10
斯威夫特3
对于任何磕磕绊绊的人来说,这是一个快捷的答案.
self.parent?.present(viewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
我有几乎相同的问题.原因是我试图在另一个上呈现"一些"控制器,并且在动画完成后我将设置的控制器设置为root.在此操作之后,所有其他控制器都会向我发出警告:" 不鼓励在分离的视图控制器上显示视图控制器 ".我解决这个警告只是设置"一些"控制器作为root而没有在开始时的任何演示.
删除:
[[self rootController] presentViewController:controller animated:YES completion:^{
[self window].rootViewController = controller;
[[self window] makeKeyAndVisible];}];
Run Code Online (Sandbox Code Playgroud)
只需在没有任何演示的情
[[self window] setRootViewController:controller];
Run Code Online (Sandbox Code Playgroud)
解决这个问题的方法之一就是如果你有childviewcontroller所以你只需要给出它的父视图上的presentviewcontroller
[self.parentViewController presentViewController:viewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
并且对于解雇使用相同的dismissview控制器.
[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
这是完美的解决方案适合我.
试试这个代码
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:<your ViewController object>];
[self.view.window.rootViewController presentViewController:navigationController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
在 Swift 4.1 和 Xcode 9.4.1 中
解决办法是
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
Run Code Online (Sandbox Code Playgroud)
如果这样写,我会遇到同样的错误
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
我收到同样的错误
Presenting view controllers on detached view controllers is discouraged <MyAppName.ViewController: 0x7fa95560Z070>.
Run Code Online (Sandbox Code Playgroud)
完整的解决方案是
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(defaultAction)
//Made Changes here
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
127220 次 |
最近记录: |