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.
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.
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.
I am presenting the alertController on the main thread.
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.
答案 0 :(得分:8)
您需要将下载结果委托给顶级视图控制器,这听起来像UITabBarController
。 UITabBarController
肯定是knows which tab is selected,或者它可以自动呈现警报。
答案 1 :(得分:1)
在从当前VC显示导航控制器或VC时写入以修复警告:
[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];
解除视图控制器时修复崩溃:
[self dismissViewControllerAnimated:YES completion:nil];
<强> OR 强>
如果你从childViewController呈现一个视图,它会给你那个警告。为避免这种情况,您可以在childViewController的父级上显示视图。
[self.parentViewController presentViewController:viewController animated:YES completion:nil];