Taxi Noi Bai Ha Noi Taxi Noi Bai Ha Noi - 1 year ago 145
iOS Question

Xcode, where to assign the segue identifier

Pardon me for beginner's question in Swift 2.
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 (<MyApps.RegViewController: 0x7fda5ae3abf0>) has no segue with identifier 'SecondViewController''

Any idea? thanks

Answer Source

Segue Identifier is not storyboard ID, storyboard ID used when you want to create a viewController based on that specific storyboard -and it have 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, ctrl + drag between the two viewControllers that you want to link. You should see:

enter image description here

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

enter image description here

The arrow that surrounded by the red rectangle is the segue.

addtional note: if you choosed "show", you have the embed your first viewController in navigationController (select your first viewController -> Editor -> Embed In -> Navigation Controller), the output should looks like:

enter image description here

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


For adding multiple segues from one viewController to many, follow the same process (ctrl + drag from the first controller to each other viewController), 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 prepareForSegue method is the solution, you can make the checking based on the segue Identifier:

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if (segue.identifier == "firstSegueIdentifier") {
            // ...
        } else if (segue.identifier == "secondSegueIdentifier") {

Hope that helped.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download