I have a view controller that is a child view controller of my window's root view controller. That child view controller has a table view and when i select a row it tells the parent view controller to present a view controller modally. The modal view controller, however, never appears. I created a bare bones test view controller that just prints viewDidLoad
and viewWillAppear
. What I notice is that when I call parentVC.present(testVC, animated:true, completion:nil)
, viewDidLoad
is run, but viewWillAppear
is not. viewWillAppear
is only then called when I interact with the UI in some way. Whether tapping, panning, scrolling or whatever.
I've spent hours trying to debug this. It doesn't seem like the main queue is blocked and I've reduced the problem to its bare bones. The modally presented view controller's viewWillAppear
is simply not called until I interact with the UI again.
What could be causing this symptom?
In comments, you mention that you're instantiating your view controller with
let vc = TestVC()
where TestVC
is presumably a (largely empty) UIViewController
subclass.
A view controller needs a view created either via either storyboard scene (using instantiateViewController
), a NIB or, in very rare cases, a view you create in loadView
(which you shouldn't be confused with viewDidLoad
).
I'd suggest creating a storyboard scene (assuming you are using storyboards), give it a storyboard ID, and then use instantiateViewController
:
let vc = storyboard.instantiateViewController(withIdentifier: "foo")
But just having a UIViewController
subclass called TestVC
and instantiating it with TestVC()
won't work.
In our discussion, you said you wanted to do this programmatically with no NIB nor storyboard. If so, use loadView
. See https://stackoverflow.com/a/37964249/1271826 for an example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.