The Situation
Let's say that I have 2 view controllers inside a navigation controller. Inside the storyboard, they would look like:
NC -> VC1 -> VC2
NC: Navigation controller
VC1: View controller one
VC2: View controller 2
So VC1 is the root view controller of the navigation controller, and VC1 is connected to VC2 via a show segue.
What must happen:
VC1 must call a function every time it opens, (e.g necessaryFunction()
). It currently calls this function in viewDidLoad()
. The problem is that when the user presses the back button in VC2 (the one on the navigation bar) and VC1 is now showing, viewDidLoad()
is not called. This means that necessaryFunction()
will not be called either.
What I am looking for:
I am looking for a way to make sure that necessaryFunction()
is called when returning to VC1 from VC2. I understand that this could be solved with a delegate, but that seems overly complicated for such a simple thing, surely there is another way to do this.
Maybe I could put necessaryFunction()
into viewWillAppear()
, but I don't think this would work.
By the way: I am coding in Swift.
EDIT: I have found this post: iOS how to detect programmatically when top view controller is popped?, but it is for objective-C, not swift.
necessaryFunction()
was called insideviewDidLoad()
– ZackviewWillAppear
is called when you pop back to VC1, so why doesn't that solve the problem? If it doesn't, you must be describing the problem incorrectly. – matt