What if you could decide exactly when and where your app goes next, making navigation smarter and smoother?
Why Programmatic navigation in iOS Swift? - Purpose & Use Cases
Imagine you have an app with many screens, and you want to move from one screen to another only when the user taps a button or completes a task.
Doing this by manually setting up segues in the storyboard for every possible path can get confusing and hard to manage.
Manually connecting screens with storyboard segues means you must predict every navigation path in advance.
This makes it slow to add new flows, and if you want to navigate based on logic (like user choices), it becomes error-prone and messy.
Programmatic navigation lets you control screen changes directly in code.
You decide exactly when and where to go next, making your app flexible and easier to update.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { // setup destination } }
let detailVC = DetailViewController() navigationController?.pushViewController(detailVC, animated: true)
It enables dynamic, flexible navigation flows that respond to user actions and app state instantly.
Think of a shopping app where after adding items to the cart, you want to navigate to checkout only if the user is logged in; programmatic navigation lets you check this and move to the right screen smoothly.
Manual storyboard segues are rigid and hard to manage for complex flows.
Programmatic navigation gives you full control over screen transitions in code.
This makes your app more flexible, easier to maintain, and responsive to user actions.
Practice
pushViewController do in iOS programmatic navigation?Solution
Step 1: Understand navigation stack behavior
In iOS, navigation uses a stack where screens are pushed and popped.Step 2: Role of pushViewController
The methodpushViewControlleradds a new screen on top, moving forward.Final Answer:
It moves to a new screen by adding it on top of the navigation stack. -> Option AQuick Check:
pushViewController = move forward [OK]
- Confusing push with pop behavior
- Thinking push resets the stack
- Mixing modal presentation with push navigation
DetailViewController onto the navigation stack?Solution
Step 1: Check method call and optional chaining
navigationController is optional, so use ? to safely call method.Step 2: Instantiate the view controller correctly
UseDetailViewController()to create an instance before pushing.Final Answer:
navigationController?.pushViewController(DetailViewController(), animated: true) -> Option BQuick Check:
Optional chaining + instance creation = correct syntax [OK]
- Forgetting to instantiate the view controller
- Not using optional chaining on navigationController
- Using wrong method name like push instead of pushViewController
func buttonTapped() {
let vc = SettingsViewController()
navigationController?.pushViewController(vc, animated: true)
}Solution
Step 1: Analyze the code behavior
The code creates a new SettingsViewController instance and pushes it onto the navigation stack.Step 2: Understand navigationController optional chaining
If navigationController exists, it will push with animation, moving to the new screen.Final Answer:
The app will navigate to the SettingsViewController screen with animation. -> Option DQuick Check:
pushViewController with valid instance = navigate forward [OK]
- Assuming crash without checking optional navigationController
- Thinking push removes current screen
- Confusing push with modal presentation
func goBack() {
navigationController.popViewController(animated: true)
}Solution
Step 1: Check navigationController usage
navigationController is optional, so it needs optional chaining with ? before calling methods.Step 2: Identify missing ? operator
The code uses navigationController.popViewController without ?, causing a compile error.Final Answer:
Missing optional chaining operator (?) after navigationController. -> Option CQuick Check:
Optional chaining needed for navigationController calls [OK]
- Forgetting navigationController is optional
- Trying to add completion handler to popViewController
- Confusing popViewController with pushViewController
Solution
Step 1: Understand navigation stack methods
popToRootViewController moves back to the first screen in the stack, popping all intermediate ones.Step 2: Compare options
popViewController removes only the top screen; dismiss is for modal; push adds a new screen, not back.Final Answer:
navigationController?.popToRootViewController(animated: true) -> Option AQuick Check:
popToRootViewController = go back to root [OK]
- Using popViewController to go back multiple screens
- Confusing dismiss with navigation pop
- Trying to push root instead of popping to it
Ready to prove it?
Pass the test and this pattern moves unknown
