Prepare for Segue in Swift

147,737

Solution 1

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.

Solution 2

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
        }
    }
}

Solution 3

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

Solution 4

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

Solution 5

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
                }

            }
     }
Share:
147,737
nikolaus
Author by

nikolaus

Updated on August 21, 2020

Comments

  • nikolaus
    nikolaus over 3 years

    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?

  • Fogmeister
    Fogmeister almost 10 years
    No you don't. The identifier is a force unwrapped property of the segue.
  • Cezar
    Cezar almost 10 years
    @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...
  • Ryan Heitner
    Ryan Heitner almost 10 years
    @Fogmeister so it seems that using the template provided by Xcode there was no unwrapping
  • Fogmeister
    Fogmeister almost 10 years
    Yeah, looks like the template for subclasses is wrong.
  • danyowdee
    danyowdee almost 10 years
    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).
  • Jakob Hartman
    Jakob Hartman almost 10 years
    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
  • danyowdee
    danyowdee almost 10 years
    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().
  • Deekor
    Deekor about 9 years
    whats does the ? after as mean?
  • Scott Byrns KCL
    Scott Byrns KCL almost 9 years
    @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.