user867662 user867662 - 1 month ago 11
Swift Question

Storyboard view controller with different function

I am new to storyboard programming. I have designed a storyboard (say SB1), it have two funcitons in its viewcontroller. I have another storyboard (SB2) and it has two buttons. From both buttons I want to show SB1, but when I press button1, then func1 should executed and if I pressing button2, then fun2 should be executed. Any suggestions.

My code below.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController = segue.destinationViewController as? StotyBoard1ViewController
where segue.identifier == "LocatorSegue" {
viewController.func1()
}
//how to invoke SB1 Func2 from here
}


@IBAction func button1Click(sender: AnyObject) {
self.performSegueWithIdentifier("SB1Segue", sender: self)
}

@IBAction func button2Click(sender: AnyObject) {
{
self.performSegueWithIdentifier("SB1Segue", sender: self)
}

Answer

You can do somthing like this:-

    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    var sender: UIButton?

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let viewController = segue.destinationViewController as? StotyBoard1ViewController {
        if segue.identifier == "SB1Segue" {
            if self.sender == button1 {
                viewController.func1()
            } else if self.sender == button2 {
                 viewController.func2()
            }
        }
   } 

    @IBAction func button1Click(sender: AnyObject) {
        self.sender = sender as! UIButton
        self.performSegueWithIdentifier("SB1Segue", sender: self)
    }

    @IBAction func button2Click(sender: AnyObject) {
    {
        self.sender = sender as! UIButton
        self.performSegueWithIdentifier("SB1Segue", sender: self)
    }