I use Apple Sample Code. Originally the method looked like this
@IBAction func doneTapped() {
favoriteCreatureDidChange?(favoriteCreature)
dismiss(animated: true, completion: nil)
}
I changed Segue Kind from Present Modally to Show (e.g. Push). Accordingly, dismiss doesn't work any more and I need to use Unwind Segue to get to my previuos VC.
It's not a case to declare an IBAction in SourceVC, because I need to execute favoriteCreatureDidChange?(favoriteCreature)
- variable, declared in the VC I am currently in.
I think I need to use unwind(for: UIStoryboardSegue, towardsViewController: UIViewController)
As I understand for for:
parameter I need to use a segue that tranferred me in the current VC.
I'm trying to get the segue's instance
let segue = UIStoryboardSegue(identifier: "showFavoriteCreaturePicker", source: DreamListViewController, destination: FavoriteCreatureListViewController)
but Xcode complains Cannot convert value of type 'DreamListViewController.Type' to expected argument type 'UIViewController'
while it's everything okay with FavoriteCreatureListViewController.
Please let me know how to pass a segue to for:
parameter without getting an error.
This initializer expects controller instances, not controller types. However, note this from the UIStoryboardSegue documentation:
You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers. You can still initiate a segue programmatically using the performSegue(withIdentifier:sender:) method of UIViewController if you want. You might do so to initiate a segue from a source that was added programmatically and therefore not available in Interface Builder.
Also, you only use this initializer in your custom segues:
If you need to initialize any variables in your custom segue subclass, you can also override the init(identifier:source:destination:) method and initialize them in your custom implementation.
As for unwind(for:towardsViewController:)
, the documentation says this (emphasis mine):
During the execution of an unwind segue, UIKit calls this method on any view controllers in the unwind path to give them an opportunity to reconfigure themselves. Container view controllers must implement this method and use to display the view controller in the subsequentVC parameter. For example, a tab bar controller selects the tab containing the specified view controller. Noncontainer view controllers should not override this method.
And, finally, the solution: you can create an unwind segue in Interface Builder and override prepare(for:sender:) in your view controller that is initiating the unwind:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
favoriteCreatureDidChange?(favoriteCreature)
}
This will be called before your unwind action in the destination view controller of the unwind segue is called.
Please also note that there is popViewController(animated:)
in UINavigationController
.