Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

storyboard - How to Pass information Back in iOS when reversing a Segue using Swift?

I have two view controllers, One and Two. I go from VC One to VC Two. On VC Two, I select some data that I store in an array. When I press the "Back" button on the navigation bar, I would like to send that array back to VC One.

What's the best way to do this using Swift & Storyboards?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...