66

I'm facing the error message:

"UIStoryboardSegue does not have a member named 'identifier'"

Here's the code causing the error

if (segue.identifier == "Load View") {
    // pass data to next view
}

On Obj-C it's fine using like this:

if ([segue.identifier isEqualToString:@"Load View"]) {
   // pass data to next view
}

What am I doing wrong?

BaSha
  • 2,356
  • 3
  • 21
  • 38
ndraniko
  • 814
  • 1
  • 8
  • 14

10 Answers10

103

This seems to be due to a problem in the UITableViewController subclass template. It comes with a version of the prepareForSegue method that would require you to unwrap the segue.

Replace your current prepareForSegue function with:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "Load View") {
        // pass data to next view
    }
}

This version implicitly unwraps the parameters, so you should be fine.

Cezar
  • 55,636
  • 19
  • 86
  • 87
  • 1
    @Fogmeister I commented out the version provided by the template and started typing `prepareForSegue`, and autocompletion suggested me the version that forces unwrap of the parameters. Who'd know... – Cezar Jun 04 '14 at 15:04
49

Swift 4, Swift 3

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "MySegueId" {
        if let nextViewController = segue.destination as? NextViewController {
                nextViewController.valueOfxyz = "XYZ" //Or pass any values
                nextViewController.valueOf123 = 123
        }
    }
}
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
38

I think the problem is you have to use the ! to unbundle identifier

I have

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if segue!.identifier == "Details" {
            let viewController:ViewController = segue!.destinationViewController as ViewController
            let indexPath = self.tableView.indexPathForSelectedRow()
            viewController.pinCode = self.exams[indexPath.row]

        }

    }

My understanding is that without the ! you just get a true or false value

Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
16

For Swift 2.3,swift3,and swift4:

Create a perform Segue at didSelectRowAtindexPath

For Ex:

   self.performSegue(withIdentifier: "uiView", sender: self)

After that Create a prepareforSegue function to catch the Destination segue and pass the value:

Ex:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

       if segue.identifier == "uiView"{

        let destView = segue.destination as! WebViewController
        let indexpath = self.newsTableView.indexPathForSelectedRow
        let indexurl = tableDatalist[(indexpath?.row)!].link
        destView.UrlRec = indexurl

        //let url =

    }
    }

You need to create a variable named UrlRec in Destination ViewController

Sandu
  • 436
  • 4
  • 8
10

Swift 1.2

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "ShowDeal") {

                if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
                    viewController.dealEntry = deal
                }

            }
     }
Scott Byrns KCL
  • 211
  • 4
  • 5
  • whats does the `?` after `as` mean? – Deekor May 08 '15 at 03:35
  • @Deekor the destinationViewController is an optional and the as? casts an optional if it is of the type being cast to. Since it is an if let, we only let the assignment occur if the optional is a DealLandingViewController, otherwise our conditional evaluates to false. This is a language feature of swift. – Scott Byrns KCL May 19 '15 at 21:46
7

Prepare for Segue in Swift 4.2 and Swift 5.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "OrderVC") {
        // pass data to next view
        let viewController = segue.destination as? MyOrderDetailsVC
        viewController!.OrderData = self.MyorderArray[selectedIndex]


    }
}

How to Call segue On specific Event(Like Button Click etc):

performSegue(withIdentifier: "OrderVC", sender: self)
M Murteza
  • 1,629
  • 15
  • 10
1

this is one of the ways you can use this function, it is when you want access a variable of another class and change the output based on that variable.

   override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {
        let something = segue.destination as! someViewController
       something.aVariable = anotherVariable
   }
alex's
  • 21
  • 1
0

Provided you aren't using the same destination view controller with different identifiers, the code can be more concise than the other solutions (and avoids the as! in some of the other answers):

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if let myViewController = segue.destinationController as? MyViewController { 
        // Set up the VC
    }
}
Taylor
  • 5,871
  • 2
  • 30
  • 64
-2

Change the segue identifier in the right panel in the section with an id. icon to match the string you used in your conditional.

-3
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if(segue!.identifier){
            var name = segue!.identifier;
            if (name.compare("Load View") == 0){

            }
        }
    }

You can't compare the the identifier with == you have to use the compare() method

Jakob Hartman
  • 346
  • 3
  • 8
  • 2
    That is an incorrect statement: Strings in Swift conform to the `Equatable` protocol, and the infix operator “==” is overridden such that, in fact, the expression `a == b` is the same as `a.isEqual(b)`. – danyowdee Jun 16 '14 at 15:21
  • You are comparing a string with NSString they are different, either way the above code works. string doesn't have a method called .isEquals or .isequalsto – Jakob Hartman Jun 17 '14 at 03:11
  • NSString and (Swift’s) String are bridged automatically behind the scenes. Try it for yourself in a playground: `let native = "Hello World"; let bridged = native as NSString` now type `native == bridged` (this yields `true`). Now type `bridged.dynamicType.description()` (this yields gibberish), and `native.dynamicType.description()`, which yields a REPL error because String.Type doesn’t respond to description(). – danyowdee Jun 18 '14 at 14:17