2

Im trying to pass data from viewController 1 to viewController2, I have 2 buttons and 1 segue(therefore there is one segue identifier) for those 2 buttons, each button when pressed should show: 1 label to show the title and 1 textView to show a definition, I am having troubles to show its own data of each word; I know it has to be the some code referencing the SENDER in the performSegueWithIdentifier, but I don't know how to do it.

I appreciate your help !!! thanks.

here is my code

class ViewController: UIViewController {

    @IBAction func AbstractionBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
    }

    @IBAction func binarySystemBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Binary System")

    }

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ShowDefinition") {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController {
                destinationViewController.titleMsg = "Abstraction"
                destinationViewController.definitionMsg = "Abstraction Definition"
            }
    } else if(segue.identifier == "ShowDefinition"){if let destinationViewController = segue.destinationViewController as? EnglishViewController {
            destinationViewController.titleMsg = "Binary System"
            destinationViewController.definitionMsg = "Binary System Definition"
            }
        }    
}
Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
Chino Pan
  • 1,658
  • 2
  • 9
  • 9
  • what excellently you want to in prepareForSegue method? – jignesh Vadadoriya Oct 10 '16 at 09:42
  • i want when pressed the "abstraction button" in the vc1 show the title and definition for abstraction and when pressed the "binary system button" show its title and definition, im working in a dictionary. – Chino Pan Oct 10 '16 at 09:45

3 Answers3

2

You have correctly passed the definition as a String in the sender parameter in performSegueWithIdentifier. You just need to use its value in prepareForSegue, but first you must cast it from AnyObject? back to a String.

Your code could be something like:

class ViewController: UIViewController {
    @IBAction func AbstractionBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
    }

    @IBAction func binarySystemBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Binary System")
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ShowDefinition") {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController {
                if let definition = sender as? String {
                    if definition == "Abstraction" {
                        destinationViewController.titleMsg = "Abstraction"
                        destinationViewController.definitionMsg = "Abstraction Definition"
                    } else if definition == "Binary System" {
                        destinationViewController.titleMsg = "Binary System"
                        destinationViewController.definitionMsg = "Binary System Definition"
                    }
                }
            }
        }
    }
}
xpereta
  • 692
  • 9
  • 21
1

Try this one

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if (segue.identifier == "ShowDefinition")
    {
        if let destinationViewController = segue.destinationViewController as? EnglishViewController
        {
             var btn = sender as! UIButton
            if btn.tag == 1
            {
                destinationViewController.titleMsg = "Abstraction"
                destinationViewController.definitionMsg = "Abstraction Definition"
            }
            else
            {
                destinationViewController.titleMsg = "Binary System"
                destinationViewController.definitionMsg = "Binary System Definition"

            }

        }
    }
}

And set button tag like this

AbstractionBtn.tag = 1
binarySystemBtn.tag = 2

Now Call for segue like this

@IBAction func AbstractionBtn(sender: AnyObject) {
performSegueWithIdentifier("ShowDefinition", sender:sender)
}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
0

Create a var named titleMsg and definitionMsg. In each @IBAction method set the appropriate title message to self.titleMsg and similarly for self.definitionMsg.

After that call,

performSegueWithIdentifier("ShowDefinition", sender: self)

And then override,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowDefinition" {
        let destinationVC = segue.destinationViewController as? EnglishViewController
        destinationVC.titleMsg = self.titleMsg
        destinationVC.definitionMsg = self.definitionMsg
    }
}

Hope that helped!

mohonish
  • 1,396
  • 1
  • 9
  • 21