Reputation: 876
I have a UITabBarController in storyboard which has a navigation controller as one of the tab bar items. When I try to do [self presentViewController:myVC animated:YES completion:NULL]
on the root view-controller of my navigation controller, the application stops responding and I get the following error/warning on the console
2015-10-16 18:21:40.175 My_App[1224:488753] Presenting view controllers on detached view controllers is discouraged <MyViewController: 0x16512b00>.
I am experiencing this issue only when the app is built using Xcode-7.0.1 and run on iOS-9. It works perfectly on iOS-8 with Xcode-7.0.1 and even on iOS-9 when built using Xcode 6.4.
Edit:
This is my view controller hierarchy just before I present myVC
from myRootViewController
.
(lldb) po [UIViewController _printHierarchy]
<LibraryTabBarController 0x7f91935e9660>, state: appeared, view: <UILayoutContainerView 0x7f9195841420>
| <UINavigationController 0x7fe46b863200>, state: appeared, view: <UILayoutContainerView 0x7fe46b54c6a0>
| | <MyRootViewController 0x7fe46b47aa40>, state: appeared, view: <UIView 0x7fe46b66f8a0>
| <MyOtherViewController 0x7f919580fd80>, state: disappeared, view: (view not loaded)
Upvotes: 0
Views: 991
Reputation: 438257
The error message suggests that the view controller you're presenting from is not (yet) in the view controller hierarchy. Perhaps you've instantiated a view controller to which self
refers and haven't yet added it to the tab bar's collection of view controllers. Perhaps you're trying to do this in prepareForSegue
. Perhaps you did addSubview
for a child view controller but neglected to do the necessary view controller containment calls (e.g. addChildViewController
). It could be a lot of issues. I'd suggest you describe a bit about how self
, itself, was presented, before we worry about how it presents other view controllers.
If you think that self
is already in the view controller hierarchy, I'd suggest we confirm that. Run the app from Xcode and before you hit this line of code that presents VC
, press the debugger's pause button:
And at the (lldb)
prompt, enter:
po [UIViewController _printHierarchy]
Or, if you pause in a Swift frame, you could enter:
expr -l objc++ -O -- [UIViewController _printHierarchy]
And you should see something like:
(lldb) po [UIViewController _printHierarchy]
<UITabBarController 0x7fcae1c19740>, state: appeared, view: <UILayoutContainerView 0x7fcae1c26730>
| <UINavigationController 0x7fcae3012c00>, state: appeared, view: <UILayoutContainerView 0x7fcae1d10b90>
| | <MyApp.ViewController 0x7fcae1c19cd0>, state: appeared, view: <UIView 0x7fcae1c2c470>
| <SecondViewController 0x7fcae1e1b860>, state: disappeared, view: (view not loaded)
Confirm that whatever view controller self
refers to in your question appears in this view controller hierarchy. If so, show us your view controller hierarchy and which one self
is.
Upvotes: 2