If you were presenting a modal view with Done and Cancel buttons (sort of like a picker), grabbing the value during an unwind segue method would probably be the easiest.
Given that you want to use the navigation controller's native Back button, the best practice would probably be to implement a protocol that VC One can conform to, and then update VC One as soon as the data on VC Two is selected. Something like:
In VCTwo.swift:
protocol VCTwoDelegate {
func updateData(data: String)
}
class VCTwo : UIViewController {
var delegate: VCTwoDelegate?
...
@IBAction func choiceMade(sender: AnyObject) {
// do the things
self.delegate?.updateData(self.data)
}
...
}
and in VCOne.swift:
class VCOne: ViewController {
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "VCTwoSegue" {
(segue.destinationViewController as VCTwo).delegate = self
}
}
...
}
extension VCOne: VCTwoDelegate {
func updateData(data: String) {
self.internalData = data
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…