1

so I am developing an app and also learning swift at the same time. I have figured out how to send data from secondViewController to the firstViewController. Essentially I have linked the "done" button to the firstViewController, so when I press "Done", it opens up a new window of the first viewController and gives the data that I have passed.

However, I am looking for a way to update the firstViewController without having another window of it opened with a different segue.

The best way to explain this is that my firstViewController has an "add" button that opens up secondViewController. This viewController has two textfields, you write stuff in it (I want stuff written in these two textbox to appear on the first viewController) & then when you press done, the secondViewController dismisses and you are back on the firstViewController.

This is the code that I am using : (in the secondViewController)

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
var destinationviewcontroller : ViewController = segue.destinationController as! ViewController
    destinationviewcontroller.LabelText = weblinklabel.stringValue
    }

Obviously labeltext is a label in the firstviewcontroller that shows what is written in those textfields in the secondViewController.

The problem is when I use storyboard and simply drag from "Done" button to the firstViewController, it opens the firstViewController in a new window with the new data/updated labeltext. I want the secondViewController to just dismiss when I press "Done" and the updated label to appear on the firstViewController without it being opened in the new window.

It's a bit difficult to explain, so if you need some more details for clarification, I will be more than happy to give it!

Thanks in advance!

O.Naeem
  • 45
  • 7
  • 1
    Sounds like you should be using [unwind Segues](http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them#12843906) – GetSwifty Nov 01 '16 at 19:22

1 Answers1

0

Yes, you should be using unwind Segues as stated here.

First, add an empty IBAction method to your firstViewController (notice you should write it directly):

@IBAction func unwindToFirstViewController (sender: UIStoryboardSegue) {
}

Once is written, now in your storyboard you can Control+drag your Done button to the Exit item in your secondViewController, like this:

enter image description here

It should pop your new IBAction method that will handle the unwind, now just implement it:

@IBAction func unwindToFirstViewController (sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? SecondViewController {
        // update whatever values you want
        // to update in firstViewController from secondViewController
    }
}
Community
  • 1
  • 1
devtbit
  • 94
  • 3
  • I don't have the `Exit` item in the storyboard because I think I am making a cocoa application and not iOS application. The unwindsegue method you are mentioning I think is for iOS apps. – O.Naeem Nov 02 '16 at 09:07