Prepare for Segue in Swift
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
}
}
}
nikolaus
Updated on August 21, 2020Comments
-
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 almost 10 yearsNo you don't. The identifier is a force unwrapped property of the segue.
-
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 almost 10 years@Fogmeister so it seems that using the template provided by Xcode there was no unwrapping
-
Fogmeister almost 10 yearsYeah, looks like the template for subclasses is wrong.
-
danyowdee almost 10 yearsThat is an incorrect statement: Strings in Swift conform to the
Equatable
protocol, and the infix operator “==” is overridden such that, in fact, the expressiona == b
is the same asa.isEqual(b)
. -
Jakob Hartman almost 10 yearsYou 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 almost 10 yearsNSString 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 typenative == bridged
(this yieldstrue
). Now typebridged.dynamicType.description()
(this yields gibberish), andnative.dynamicType.description()
, which yields a REPL error because String.Type doesn’t respond to description(). -
Deekor about 9 yearswhats does the
?
afteras
mean? -
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.