14

Pardon me for beginner's question. I know I can switch to another screen (ViewController) like this

self.performSegueWithIdentifier ("SecondViewController", sender: self)

but I can't seem to find where to assign my 2nd screen the id, I just find Storyboard ID, is that it?

I've already tried, only received a crash with the following error:

Receiver () has no segue with identifier 'SecondViewController'

Any idea? thanks

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • If you are a Swift beginner, you really should learn Swift 3, not Swift 2, and use Xcode 8, not Xcode 7. Only use the older tools if you have been given a specific job to do with those specific tools. – rmaddy Oct 06 '16 at 19:40
  • Hi, because the tutorial I bought a while ago was using Swift 2, I'm following it – EyeQ Tech Oct 06 '16 at 19:42
  • You need a modern tutorial. You are wasting your time learning an outdated form of the language. A lot has changed in Swift 3. Start with Apple's free book - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/index.html – rmaddy Oct 06 '16 at 19:44
  • It probably is better to learn Swift 3 if you're starting from scratch but the segue handlers haven't changed drastically between v 7 and v 8 – NiallJG Oct 06 '16 at 21:22
  • https://stackoverflow.com/a/41887007/5461400 – Harshal Valanda May 09 '18 at 07:14

6 Answers6

34

Segue Identifier is not the same as storyboard ID, storyboard ID used when you want to create a View Controller based on that specific storyboard -and it has to be unique, unlike the segue identifier-.

If you already know how to create a segue, you can skip this part.

Adding a segue between two viewControllers:

From the Interface Builder, press the ctrl and drag between the two View Controllers that you want to link (make sure that you are dragging from the view controller itself, not the its main view). You should see:

enter image description here

Choose the "Show" -for instance-, the output should look like this:

enter image description here

As shown above, the arrow that surrounded by the red rectangle is the segue.

Additional note: if you selected the "Show" option, you have to embed your first view Controller in a Navigation Controller (select your first viewController -> Editor -> Embed In -> Navigation Controller), the output should looks like:

enter image description here

Because the "Show" means pushing into a navigation controller stack.

Assigning an identifier for the segue:

Select the segue, from the attribute inspector you'll see "Identifier" text field, that's it! make sure to insert the exact same name that used in performSegueWithIdentifier.

If you don't know where to find the attribute inspector, it is on the top right looks like:

enter image description here


Furthermore:

For adding multiple segues from one View Controller, follow the same process (ctrl + drag from the first controller to each other View Controller), the output should looks like:

enter image description here

In this case, you might face the issue how to recognize which segue has been performed, overriding prepare(for:sender:) method is the solution, you can make the checking based on the segue identifier property:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "firstSegueIdentifier") {
        // ...
    } else if (segue.identifier == "secondSegueIdentifier") {
        //...
    }
}

which would be the name that you have added to the segue in the storyboard.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • For me, ctrl+clicking from one viewcontroller to another just moves the first viewcontroller to cover the second on the storyboard. – Clinton J Sep 13 '17 at 18:05
  • 1
    @chodobaggins first, select the viewcontroller itself and ctrl + drag from the yellow circle that appears on top of it to the desired controller. – Ahmad F Sep 13 '17 at 20:24
4

In your code

self.performSegueWithIdentifier ("SecondViewController", sender: self)

the string "SecondViewController" is looking like storyboard id . At the same place you have to write the segue identifier , not storyboard id .

Follow the Screenshot and assign segue identifier name by clicking on the segue on right top bar field. you can do like this

self.performSegueWithIdentifier ("WriteSegueIdentifierName", sender: self)

enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Harish Singh
  • 765
  • 1
  • 12
  • 23
1

Similar to the answers provided, here's what it would look like in Xcode 12:

  1. Select the segue link in Main Storyboard view

Segue Link

  1. Navigate to the Attribute Inspector and add an Identifier (if one does not exist)

Xcode 12 Attribute Inspector

  1. Add the Identifier to your code. Done!

Add to code

gyos23
  • 11
  • 3
0

When you link a View controller to another View controller in the storyboard, in the link between them you need to assign a segue identifier i.e "SecondViewController" only then your code will work.

Alternatively, you can also show another view controller through storyboard id using self.storyboard.instantiateViewControllerWithIdentifier("//storyboard id of that view controller") and then either use present/show view controller.

Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
0

You can initiate viewController like this:

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
self.present(viewController, animated: false, completion: nil)
Abuzar Manzoor
  • 379
  • 4
  • 13
0

Show Xcode screenshot

enter image description here

Click on Present Modally segue label... and on the Attributes Inspector input Identifier

Iva
  • 2,447
  • 1
  • 18
  • 28