Performing Segues in iOS Programmatically

Storyboards in iOS were introduced in iOS 5.  They are essentially a way to layout all of your views and they way that they will interact with each other.  It allows you to see an overview of all of your views at once.  They also make it really easy to create certain views without code that used to require code.  This blog post won’t go into those details.  It assumes that you have worked with a storyboard without coding and now may need to introduce some code to change the flow of your application.

The way a storyboard works is you connect views together via segues.  These segues will either push (if you are using a navigation controller) or modally add a view onto the phone.  The way you create the segue is by ctrl-clicking a button on one view and dragging it onto another then selecting either push or modal depending on if you are using a UINavigationController or not.

Once your segue is created you can run the application.  When you click on the button, the new view will be displayed.  Now, let’s say that you wanted to trigger that segue in a way which requires no user interaction.  How would this be accomplished?  It is accomplished fairly easily and this is how.

First we need to give our segue a name so we can identify it in the view controller.  Below is a screenshot of a simple segue I have created and called it ‘MySegue’.

Now in the view controller that contains the button for the segue, add this line of code to the place in your code where you would want that transition of views to take place

[self performSegueWithIdentifier: @"MySegue" sender: self];

This tells our view to look for a segue labeled MySegue and invoke that segues action.  Now, if there are values on the new view controller that you are going to be using after the segue that need to be passed from the first view controller, this can be done by adding a simple method and doing a quick check on the segues identifier.  Add this code anywhere in your initial view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"])
    { 
        NewViewController *vc = [segue destinationViewController];
        vc.dataThatINeedFromTheFirstViewController = self.theDataINeedToPass;
    }
}

That’s essentially all you need code wise to spin off segues programmatically.  Possible use cases for doing this would be if you are doing something like a login where the user will click a button but when they do you don’t want to leave the view right away as you would need to check their credentials before continuing. 

Since the button associated with the segue you are going to call programmatically may not actually be used by the user, the button can just be hidden in the background.  Another thing to note is that it is possible to create segues that segue to the same view controller.  In this instance you would ctrl-click the button and drag it back onto the same view.  A possible use case for this would be if you need to load the same view with entirely different data and if you create your views programmatically, it may be the easiest solution to starting with a clean slate.

Go ahead and try this out and if you have any questions, feel free to let me know.  Or, if you have had experience with segues and you might have some tips or tricks, let us know in the comments!

Comments

Returning visitor? Please login or register.

Thanks this helped my out a lot!

Casper Posted on: Apr 02, 2013 at 09:34 AM

Thank you for this post!
simple, straightforward, helpful

vvolf Posted on: May 30, 2013 at 02:34 PM

Is it possible to go to a new View controller with out drawing a story board segue?the two view controllers wii not be connected with a segue any way….

Sauvik Dolui Posted on: Sep 09, 2013 at 01:07 AM

Hi Sauvik,

I believe you can but it would be outside of the storyboard.

Craig Isakson Posted on: Sep 09, 2013 at 08:05 AM

can i do prepare for segue condition with should performs segue with identifier method in order to pass values in conditional manner….if yes pls send me the sample program using storyboard as much as possible ...thank you in advance;)

Hi Sauvik Posted on: Oct 10, 2013 at 06:42 AM

Hi Sauvik,

When you instantiate the new view controller in prepareForSegue, you can set values for variables on that new view controller.  The sample code provided shows this:

NewViewController *vc = [segue destinationViewController];
vc.variableOne = self.theDataINeedToPass; //this is a variable on your current view controller before you have moved to the new view controller.
vc.variableTwo = self.theDataINeedToPass2;

Let me know if this helps!

Thanks!
Craig

Craig Isakson Posted on: Oct 10, 2013 at 07:35 AM

It seems that the only point of the storyboard is to give the segue a name. So you could get rid of the storyboard entirely, and do everything programmatically? What would that look like?

businessCard Posted on: Oct 22, 2013 at 09:27 AM

At that point, you could just use a navigation controller or standard view controllers.

Craig Isakson Posted on: Oct 22, 2013 at 09:50 AM

Hi Craig Isakson ,
    Thank you for your code to set @property values from previous view controller in -(void) prepareForSegue:segue…This avoids the execution of a method of next view controller before segueing.

Thank You again ,
Sauvik Dolui
Innofied Solution Pvt. Limited
Salt Lake,Kolkata,India

Sauvik Dolui Posted on: Oct 22, 2013 at 10:25 AM

Leave A Comment

Please help us stop spam by answering the question below: