Xcode, Where to Assign the Segue Identifier

Xcode, where to assign the segue identifier

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:

Sample Image

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

Sample Image

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:

Sample Image

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:

Sample Image


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:

Sample Image

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.

Change segue identifier name programmatically swift

You can't change segue names programmatically. Because a segue defines a transition between two view controllers in your app’s storyboard file and you can rename it in your storyboard.

You can define more than one segues between your view controllers and call segues programmatically like this:

performSegue(withIdentifier: "yourSegueID", sender: nil)

For your problem:

  1. You can create two segues between your view controllers, and give
    two different segue identifier names to them (eg. englishSegue and
    arabicSegue).
  2. Create a variable for your segue identifier. (You will use it to
    select which of your segue should be triggered.)
  3. Control your language in your view controller (eg. in your
    viewDidLoad function or where you should control it)
    programmatically and set your language variable as English or Arabic.
  4. After that you can trigger it anywhere you want it. For example in a
    button click:

    if (yourIdentifier == "English") {
    performSegue(withIdentifier: "englishSegue", sender: nil)
    } else if (yourIdentifier == "Arabic") {
    performSegue(withIdentifier: "arabicSegue", sender: nil)
    }

How to identify the segue identifier on destination view controller?

You have tell the second view controller what to do upon the first view controller selected option (signin or signup). I would assume that you could do this by simply declaring a flag and send it to the second view controller, for instance:

Declare a boolean variable in your second view controller (let's say shouldBehavesAsLogin) which means if selection is login it should be true:

// Controller that could represents signin or signup:
class SecondViewController: UIViewController {
//...

var shouldBehavesAsLogin = false

// ...
}

thus you could determine what is the value that should be assigned to it based on which button tapped, first view controller:

class FirstViewController: UIViewController {
// ...

private var isLoginTapped = false
@IBAction func signinTapped(sender: UIButton) {
isLoginTapped = true
}

@IBAction func signupTapped(sender: UIButton) {
// nothing to do here, isLoginTapped is false by default...
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MySegue"{
if let nextViewController = segue.destination as? SecondViewController {
nextViewController.shouldBehavesAsLogin = isLoginTapped
}
}
}

// ...
}

Thus all you have to do is to check the value of shouldBehavesAsLogin whether is it true to let the controller behaves as login or false to do the opposite.


Additional Tip:

If the purpose of adding IBActions for each button is just navigating to the second view controller, I would suggest to let both of the buttons to have the same IBAction, but you should let the sender to be of type UIButton instead of Any, thus you could do -for instance-:

@IBAction func aButtonTapped(sender: UIButton) {
// do the default behvior for both signin and signup (navigate to the second controller)

// signinButton is the button you tap for navigating to second controller to behaves as signin
isLoginTapped = sender === signinButton ? true : false
}

How to detect which segue identifier activated current view controller

Probably you should create on your "current view controller" a property to store the name of the segue and then on the controller which uses the segue to instantiate your "current view controller" you assign it before fire the segue execution:

ObjectiveC:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"YourSegueName"]) {

// Get destination view
CurrentVC *vc = [segue destinationViewController];

// Get button tag number (or do whatever you need to do here, based on your object
vc.segueName = @"YourSegueNam";
}}

Swift 3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueName"
{
if let destinationVC = segue.destination {
destinationVC.segueName = segue.identifier
}
}
}

Is that what you need? Let me know.
Anyway I still do not know why you want to do that.

self.performSegue cannot find storyboard-segue-ID even though it has been named

You need to have the segue from LiveController, not from Navigation Controller

Segue and Button programmatically swift

Create seuge

Create Seuge

Assign identifier

Sample Image

and your button target

 @IBAction func button_clicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueToNext", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToNext" {
if let destination = segue.destination as? Modo1ViewController {
destination.nomb = nombres // you can pass value to destination view controller

// destination.nomb = arrayNombers[(sender as! UIButton).tag] // Using button Tag
}
}
}

How to search Segue Identifier in storyboard?

As suggest by Drux:

The best way i found is search Segue Identifier in the XML source code (Open As | Source Code) for the name of the viewcontroller and take it from there.

Swift segue identifier always nil, Storyboard ID not working

Select the segue on the storyboard, and this is the segue identifier.
Sample Image



Related Topics



Leave a reply



Submit