Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepare for Segue in Swift

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?

like image 315
nikolaus Avatar asked Jun 04 '14 14:06

nikolaus


People also ask

What does prepare for segue do?

The segue object contains information about the transition, including references to both view controllers that are involved. Because segues can be triggered from multiple sources, you can use the information in the segue and sender parameters to disambiguate between different logical paths in your app.

What is perform segue in Swift?

Swift version: 5.6. Segues are a visual way to connect various components on your storyboard, but sometimes it's important to be able to trigger them programmatically as well as after a user interaction.

How do you pass data in unwind segue?

You create the unwind segue as per usual - drag to the exit icon in the scene. Now in the object inspector on the left you will see the unwind segue listed below the view controller, first responder and exit icons. You can click on the unwind segue and give it an identifier in the inspector on the right.


2 Answers

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.

like image 162
Cezar Avatar answered Sep 22 '22 05:09

Cezar


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         }     } } 
like image 43
Mohammad Zaid Pathan Avatar answered Sep 25 '22 05:09

Mohammad Zaid Pathan