Programmatic navigation lets your app move between screens by code, not just by tapping buttons in the design. This helps you control where users go based on what they do.
Programmatic navigation in iOS Swift
let nextVC = NextViewController() navigationController?.pushViewController(nextVC, animated: true) // To go back: navigationController?.popViewController(animated: true)
Use pushViewController to go forward in the navigation stack.
Use popViewController to go back to the previous screen.
let detailVC = DetailViewController() navigationController?.pushViewController(detailVC, animated: true)
navigationController?.popViewController(animated: true)
let profileVC = ProfileViewController()
profileVC.userId = 123
navigationController?.pushViewController(profileVC, animated: true)This app starts with a white screen and a button labeled "Go to Second Screen". When tapped, it moves to a gray screen with a "Go Back" button. Tapping "Go Back" returns to the first screen.
import UIKit class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white let button = UIButton(type: .system) button.setTitle("Go to Second Screen", for: .normal) button.addTarget(self, action: #selector(goToSecond), for: .touchUpInside) button.frame = CGRect(x: 50, y: 200, width: 220, height: 50) view.addSubview(button) } @objc func goToSecond() { let secondVC = SecondViewController() navigationController?.pushViewController(secondVC, animated: true) } } class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .lightGray let button = UIButton(type: .system) button.setTitle("Go Back", for: .normal) button.addTarget(self, action: #selector(goBack), for: .touchUpInside) button.frame = CGRect(x: 50, y: 200, width: 220, height: 50) view.addSubview(button) } @objc func goBack() { navigationController?.popViewController(animated: true) } } // In AppDelegate or SceneDelegate, set FirstViewController as root inside UINavigationController
Make sure your view controller is inside a UINavigationController to use push/pop navigation.
You can pass data to the next screen by setting properties before pushing it.
Animated transitions make navigation feel smooth and natural.
Programmatic navigation moves between screens using code.
Use pushViewController to go forward and popViewController to go back.
It helps control app flow based on user actions or logic.
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
