I’m an Android developer working on an iOS version of our app. I need to know how to achieve behavior similar to startActivityForResult on Android. I need to show a new view controller, then have control return to the previous view controller when the new view controller is closed. I also need a callback method to be triggered at that time.
How can I achieve this in iOS?
There are a couple ways, so mostly you do this yourself with various patterns. You can set up a navigation controller in the app delegate as follows:
Then when you want to present a new vc you can do this:
To go back do this:
As far as a callback goes one way to do this is to make a protocol like this somewhere in your project:
Then make each view controller you want a callback to be triggered in a delegate aka:
Finally when you present a new vc assign it as a delegate:
then when you dismiss the ovc, make this call
And in the rootVC, which conforms to the protocol you made, you just fill out this method:
That you just made a call to. This requires a lot of setup but other options include looking into NSNotifications and blocks which can be simpler depending on what your doing.