2

I have to functions, both of them trigger performSegueWithIdentifier with the same segue. But depending of which function was called I need to pass different parameters in prepareForSegue.

Some thing like

func first() {
  // do some stuff
  performSegueWithIdentifier("My segue", sender:AnyObject?)
}

func second() {
  // do some stuff
  performSegueWithIdentifier("My segue", sender:AnyObject?)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "My segue" {
        let destination = segue.destinationViewController as! MyController
        if functionFirstWasCalled {
           destination.property = value
        } else if functionSecondWasCalled {
           destination.property = anotherValue
        }
    }
}

Surely, I can do this by setting booleans from second() and first() and then checking them in prepareForSegue - but maybe there is some more elegant way to do this ?

Alexey K
  • 6,537
  • 18
  • 60
  • 118

3 Answers3

1

In objective -c you would do:

     [self performSegueWithIdentifier:@"segueNAme" sender:@"firstMethod"];

and you can access this message in the prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([sender isEqualToString:@"firstMethod"]) {
        //firstMEthod called the segue
    }
}

The swift equivalent I think would be:

 self.performSegueWithIdentifier("segueNAme", sender: "firstMethod")

and

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
    if (sender == "firstMethod") {
        //firstMEthod called the segue
    }
}

My suggestion would be to instead of sending a plain string , send a dictionary type object that contains the methodName, className and some other params useful for future debugging.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

All you have to do is send a flag by sender attribute, something like this:

func first() {
    performSegueWithIdentifier("My segue", sender:true)
}

func second() {
   // do some stuff
   performSegueWithIdentifier("My segue", sender:false)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

  if segue.identifier == "My segue" {
     let destination = segue.destinationViewController as! MyController
     let isFirstFunctionCalled = sender as! Bool //  cast sender to bool
     if isFirstFunctionCalled {
         destination.property = value
     } else {
         destination.property = anotherValue
     }
   }

}
-1

Simply set parallel properties in this View Controller which you then pass to the destination View Controller based on the function called. i.e:

var a = ""
var b = 0

func first() {
  // do some stuff
  a = "first function determined this variable."
  b = 1
  performSegueWithIdentifier("My segue", sender:AnyObject?)
}

func second() {
  // do some stuff
  a = "second function determined this variable."
  b = 182
  performSegueWithIdentifier("My segue", sender:AnyObject?)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "My segue" {
        let destination = segue.destinationViewController as! MyController
        if functionFirstWasCalled {
           destination.property = a
        } else if functionSecondWasCalled {
           destination.property = b
        }
    }
}
Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75