0

i have two view controllers and i want to pass data from 1st view to the second when i press ok button and move to the 2nd view without passing any data when i press cancel

my storyboard

i wrote this code

@IBAction func submitL() {
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        var third = segue.destinationViewController as ViewController
        third.lastName = theLastName.text
        third.name = theName.text
    }



}

this code didn't work

Tevfik Xung
  • 968
  • 3
  • 10
  • 18

1 Answers1

1

Take your prepareForSegue() method out of your submit function for starters. Look up "performSegueWithIdentifier" after that and you'll see how this all works.

self.performSegueWithIdentifier("nameOfYourSegue", sender: self)

Sending data with Segue with Swift

Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • the ok button works but i'm getting this message "2015-01-02 21:07:42.364 Test[676:18062] Warning: Attempt to present on whose view is not in the window hierarchy!" also when i press the cancel button the previous data in the 1st view disappears – Tevfik Xung Jan 02 '15 at 19:11
  • There are multiple ways you can accomplish this and the one you choose is personal preference. In essence you are simply firing an action on button click that either sends or doesn't send the string value of those textFields to the viewController that you grab a reference of in your prepareForSegue function. 2 different actions, 1 action based on the sender, storing to CoreData then pulling that value on viewWillAppear, etc.; this can be tackled numerous ways. Master how segues work in general by going through tutorials or Apple Docs and it'll be MUCH clearer. Get a reference, pass the object. – Mark McCorkle Jan 02 '15 at 19:22