How To Trigger A Segue Programmatically In iOS

mainYou have your storyboard ready and you have created the necessary segues. Great! Now how do we trigger these segues programmatically? There are many different ways to do it. One of the nicer ways to do this is by connecting two scenes in a storyboard using a segue that is not attached to an action. After that, you can programmatically trigger the segue inside your view controller. The way you do this is by control-dragging from the file owner icon at the bottom of the storyboard scene to the destination scene.  

A popup will appear that will ask for an option in “Manual Segue”; pick “Push” as the type. Tap on the little square and make sure you’re in the Attributes Inspector. Give it an identifier which you will use to refer to it in code. Next, we are going to trigger the segue using a button. You can use any type of button to achieve this. In our case, let’s pick a programmatic bar button item. We need to create a button on the navigation bar in viewDidLoad:

UIBarButtonItem *myBarButton = [[UIBarButtonItem alloc] initWithTitle:@"My Title" style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction:)];

self.navigationItem.rightBarButtonItems = @[myBarButton];

If you want to keep the big “plus” sign instead of “My Title”, then do this instead:

UIBarButtonItem *myBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(buttonAction:)];

self.navigationItem.rightBarButtonItems = @[myBarButton];

Note that the selector is “buttonAction”. This is the function that will be called if you press the button. We want this function to trigger the segue and go to the destination scene. So we need to write a void() method for that button and within that method, we will call the segue like this:

-(void)buttonAction:(id)sender
{
    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
}

The sender parameter is required to identify the button when prepareForSegue is called. Here, prepareForSegue is the framework method where you will instantiate your scene and pass it whatever values it will need to do its work. For a master-detail application, the method looks something like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegueName"]) 
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}

————————————————————————————————-

5 thoughts on “How To Trigger A Segue Programmatically In iOS

  1. Is it possible to create a segue without the use of a button and without the views being part of a Navigation Controller.

    Example: App loads and if no user is logged in, it programmatically segues to a login screen.

    Thanks in advance.

    1. Yes, but perhaps it might be better to use a non segue mechanism (Ie pushing the view onto a navcontroller or something)

Leave a reply to Prateek Joshi Cancel reply