xcode 警告:不鼓励在分离的视图控制器上展示视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40821786/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:36:40  来源:igfitidea点击:

Warning: Presenting view controllers on detached view controllers is discouraged

iosobjective-cxcodecompiler-warningsuialertcontroller

提问by Scooter

My situation with this is different than every other example I have been able to find on here. I have a tab based app. On one of the tabs a user is able to press a button that downloads several files from a web server all at once.

我的情况与我在这里找到的所有其他示例都不同。我有一个基于标签的应用程序。在其中一个选项卡上,用户可以按下一个按钮,一次从 Web 服务器下载多个文件。

I make use of NSOperation to perform each of these downloads so that I can utilize the built in dependencies. The downloads are all occurring on a background thread so the app remains responsive. When the final download is complete I put an alertController on screen letting the user know that they are complete.

我利用 NSOperation 来执行这些下载中的每一个,以便我可以利用内置的依赖项。下载都发生在后台线程上,因此应用程序保持响应。最终下载完成后,我在屏幕上放置了一个 alertController,让用户知道它们已完成。

If the user has selected a different tab when the alert controller is presented I get the warning: "Presenting view controllers on detached view controllers is discouraged"

如果用户在显示警报控制器时选择了不同的选项卡,我会收到警告:“不鼓励在分离的视图控制器上展示视图控制器”

If they are still on the same tab that started the downloads then I don't get the warning. I tried replacing:

如果它们仍然在开始下载的同一个选项卡上,那么我不会收到警告。我尝试更换:

[self presentViewController:alertController animated:YES completion:nil];

with

[self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];

but the result is that the alertController is never presented.

但结果是 alertController 永远不会出现。

I am presenting the alertController on the main thread.

我在主线程上展示了 alertController。

I have no way of predicting what tab view controller the user will be on when the downloads complete, and would really like to get rid of this warning.

我无法预测下载完成后用户将使用哪个选项卡视图控制器,并且真的很想摆脱这个警告。

I am developing on macOS and Xcode 8 with Obj-C.

我正在使用 Obj-C 在 macOS 和 Xcode 8 上进行开发。

回答by i_am_jorf

You need to delegate the result of the download to the top-level view controller, which sounds like your UITabBarController. The UITabBarControllercertainly knows which tab is selected, or it can present the alert on itself.

您需要将下载结果委托给顶级视图控制器,这听起来像您的UITabBarController. 在UITabBarController肯定知道选择哪个选项卡,也可以对自己表现的警报。

回答by Harshal Wani

Write to fix warningon presenting navigation controller or VC from current VC:

写入以修复当前 VC 中显示导航控制器或 VC 的警告

[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];

To fix crashwhile dismissing view controller:

在关闭视图控制器时修复崩溃

[self dismissViewControllerAnimated:YES completion:nil];

OR

或者

If you present a view from a childViewController it will give you that warning. To avoid this, you can present a view on childViewController's parent.

如果您从 childViewController 展示一个视图,它会给您那个警告。为了避免这种情况,您可以在 childViewController 的父级上显示一个视图。

[self.parentViewController presentViewController:viewController animated:YES completion:nil];