Programmatically navigate to another view controller/scene
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Programmatic navigation in UIKit is mostly about choosing the right transition type for the flow you are building. The API call is easy once that decision is clear: push when you are drilling deeper in a navigation stack, present when the new screen is a separate modal task, and replace the root controller only for major app-state changes.
Push When the Screen Belongs in the Navigation Stack
If the next screen is part of a hierarchical flow, use pushViewController on a UINavigationController.
This preserves the normal back button and stack history. It also means the current controller must actually be embedded inside a navigation controller. If navigationController is nil, the push will do nothing.
That is one of the most common sources of confusion when "programmatic navigation" appears broken.
Present Modally for Separate Tasks
If the new screen is a self-contained task such as settings, login, or a short form, modal presentation is often the better choice.
Wrapping the modal scene in its own navigation controller is a common pattern when that modal flow may push its own internal screens.
Instantiate Storyboard Scenes Explicitly
If you are using storyboards and want to navigate without segues, instantiate the destination view controller by identifier.
This is often easier to manage than mixing storyboards, segues, and code-driven transitions in inconsistent ways.
Replace the Root for App-State Changes
Sometimes the transition is not part of normal navigation at all. For example, moving from the login flow to the main app shell is usually cleaner as a root-controller replacement.
This avoids piling modals or extra pushes onto stacks that no longer represent the real app state.
Pass Data Before Navigating
When a destination needs context, pass it explicitly before the transition.
For simple flows, setting a property is fine. For larger apps, route models, dependency injection, or coordinators often scale better.
Prevent Duplicate Transitions
Programmatic navigation can break if the user taps a button repeatedly and triggers the same transition several times before the animation completes.
This is a practical fix for a very common class of intermittent navigation bugs.
Use Coordinators When Flows Get Complex
Once the app has many scenes and cross-cutting routes, moving navigation logic into a coordinator can reduce coupling.
You do not need this pattern for tiny apps, but it helps once routing starts spreading across many controllers.
Common Pitfalls
The most common pitfall is trying to push from a controller that is not inside a navigation controller. Another is using modal presentation when the user really expects stack-based back navigation.
Teams also often mix root replacement, pushes, and presents without deciding which one owns each flow. That leads to confusing back behavior.
Finally, if you programmatically instantiate storyboard scenes, keep identifiers centralized. Random string identifiers scattered across files are easy to break.
Summary
- Use
pushViewControllerfor hierarchical stack navigation. - Use
presentfor separate modal tasks. - Replace the root controller for major app-state transitions such as login to main app.
- Pass route data explicitly before navigating.
- As routing grows, centralize it instead of leaving every controller to navigate itself differently.
Related reading
- Programmatically obtain the phone number of the Android phone
- Programmatically open Maps app in iOS 6
- Programmatically retrieve memory usage on iPhone
- Programmatically scroll a UIScrollView
- Programmatically Select all text in UITextField
- Programmatically selecting text in an input field on iOS devices mobile Safari
- Programmatically set image to UIImageView with Xcode 6.1/Swift
- Programmatically set left drawable in a TextView
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.