The common way for passing data back is to use delegation. Delegation in Swift is done using protocols. So, for example, you declare the following protocol (please, use meaningful names for protocol and properties, the following is just an example):
protocol COrDDelegate {
func willReturn(someString: String)
}
Then, you add corresponding delegate properties to C and D view controllers:
class C: UIViewController {
weak var delegate: COrDDelegate?
...
}
(the same goes for D). You'll have to use weak
modifier to avoid retain cycles. After that, call delegate's method right before dismissing the view controller:
func dismissMe() {
self.delegate?.willReturn(someString: "Meaningful data")
self.dismiss(animated: true, completion: nil)
}
In B, you implement the protocol:
extension B: COrDDelegate {
func willReturn(someString: String) {
print(someString)
}
}
and assign the delegate property to C or D in prepare(for:sender:)
segue preparation method (just like you are probably doing for email
):
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let c = segue.destination as? C {
c.email = self.email
c.delegate = self
}
}
Hopefully, I was able to convey the general idea. Good luck!