25

I encountered a crash while testing my app. The following image is my storyboard:

storyboard

I have the code in View Controller:

@IBAction func unwindToHomeScreen(segue:UIStoryboardSegue) {

}

The view of "Add new Item" have a "Cancel" button, I controlled-drag it to the "Exit" icon at the top and link to unwindToHomeScreen, it works fine.

The "Add new Item" has a class AddTableViewController and the code I wrote inside:

@IBAction func save() {
    // Execute the unwind segue and go back to the home screen
    performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}

I controlled-drag the "Save" button to the func, but the app crash when I click the button

I can use dismissViewControllerAnimated(true, completion: nil) instead, but I just wonder why performSegueWithIdentifier can't work?

I read the code at dropbox.com/s/hpybgg9x67rtqng/foodpinstatictable.zip?dl=0 and try to make one and using performSegueWithIdentifier like this example for practicing, I didn't see the segue identifier at her, what is the difference?

Let's_Create
  • 2,963
  • 3
  • 14
  • 33
Ian
  • 291
  • 1
  • 3
  • 9
  • Did you actually give your segue an identifier in the storyboard? The name of the function in the controller you're going back to is not an identifier. – rdelmar Jan 11 '15 at 17:05
  • 1
    I solved the problem, when I control-drag the "Cancel" to the "Exit" icon and link to "unwindToHomeScreen", I will get the "Unwind segue to Scene Exit Placeholder" show on the Document outline under the "Add New Item Scene", and then I get it the Identitier as "unwindToHomeScreen" – Ian Jan 12 '15 at 01:53

8 Answers8

69

You haven't actually given that segue an identifier. unwindToHomeScreen is a function that you can call; what you're looking for is your segue identifier, which is set in Interface Builder like this:

Set an identifier

In this example, I have a button wired to the next view via Interface Builder like you describe ("Interface Builder") and a button that is wired to this IBAction in my view controller ("Programmatically"):

@IBAction func goToNextView(sender: UIButton!) {
    self.performSegueWithIdentifier:("go", sender: self)
}

In any case, what you're missing is the actual identifier, which can be set in the attributes of a segue created in Interface Builder.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
6

Swift 4:

Sometimes we must clean the build folder and then try again.

Worked for me after ctrl-dragging the new segue and giving it a name, then using it programatically as:

performSegue(withIdentifier: "goToMyNewViewController" , sender: self)
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
Kristian
  • 2,071
  • 23
  • 15
3

I found that because I renamed my view controller, I needed to delete and recreate the segue.

Phil Hudson
  • 3,819
  • 8
  • 35
  • 59
1

A possible issue with iOS 12 and earlier (iOS 13 seems not to suffer from the same issue) might come from the fact that performSegue(withIdentifier:,sender:) is called from the view controller viewdidLoad() callback.

You absolutely must invoke it from the viewDidAppear(_ animated: Bool) callback in order to avoid the question-mentioned crash.

That will certainly save hours of hair-puling…

Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95
0

in my case reorder the Embed in

Tab bar controller Navigation controller vc (contains button - tapping button initiates a "show, e.g. push" segue)

Too fix the title in the navigationBar

Espacucho
  • 1
  • 3
0

I had the same issue.

All I did was:

I selected the segue indicator in the storyboard.

Deleted it.

Then created a new segue with the same identifier. Boom, it works.

Waheed
  • 41
  • 2
  • 8
0

I had it all wired up correctly. It was just that the viewController I was segueing from, was NOT instantiated from the storyboard. I had it instantiated pragmatically.

I initially had it written as:

let vc = DeviceVC()

Had to change it to:

let sb = UIStoryboard(name: "Main", bundle: Bundle(for: DeviceVC.self))
let vc = sb.instantiateViewController(identifier: "DeviceVC") as! DevieVC
mfaani
  • 33,269
  • 19
  • 164
  • 293
-1

for me --> click on the relation-arrow between view controllers and then select attribute inspector, there is identifier give name and select push class done...

Sumit
  • 1,022
  • 13
  • 19