Sundog Expert Craig Isakson

Craig Isakson

Technical Architect

More Posts

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!

Posted in: Mobile, Software, Software Development, Technology, Technical Strategy