4thSpace
4thSpace

Reputation: 44352

How to create variable for segue name?

If I have several segues from one viewcontroller to another, is there some way to assign their identifiers into a variable so the variable can be referenced rather than a literal string, as shown below?

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"%@", segue.identifier);
    if([segue.identifier isEqualToString:@"someSegueName"]){
    //push to view controller
    }
}

I'd like it so if the identifier name changes, it will update the variable assignment.

Upvotes: 1

Views: 105

Answers (2)

MIWMIB
MIWMIB

Reputation: 1497

You could save it to a variable when that method is called the first time in this way: -

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"%@", segue.identifier);
    if([segue.identifier isEqualToString:@"someSegueName"]){
        someVariable = seague; // This is my addition.
        //push to view controller
    }
}

But, I would recommend you to save the segue names in a constant string in your class so that you can use that repeatedly without making mistakes. This way, changing the segue name will also be easier later.

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"%@", segue.identifier);
    if([segue.identifier isEqualToString:someVariable]){ //I've changed this line
        someVariable = seague;
        //push to view controller
    }
}

Upvotes: 0

creative_rd
creative_rd

Reputation: 695

I do not excatly got it why are you trying to do it so ? You can try below logics if it suits you.

You can try with enum by giving enum values for the different segues as strings and using that strings for the identifier.

Can store in the Array and use accordingly

Another way would be by creating constants for the each and every identifier.

Upvotes: 1

Related Questions