Programmatic navigation means moving between screens (view controllers) using code instead of storyboard segues. It lets you control when and how to show new screens.
Click to reveal answer
beginner
Which method is used to push a new view controller onto the navigation stack?
You use navigationController?.pushViewController(_:animated:) to show a new screen by adding it on top of the current one.
Click to reveal answer
beginner
How do you present a view controller modally in Swift?
Use present(_:animated:completion:) on the current view controller to show another screen that covers it, like a popup or full screen.
Click to reveal answer
intermediate
What is the difference between pushing and presenting a view controller?
Pushing adds a screen to the navigation stack with a back button. Presenting shows a screen modally without a back button, often for temporary tasks.
Click to reveal answer
intermediate
Why is it important to check if navigationController is not nil before pushing?
Because pushing only works if the current view controller is inside a navigation controller. If nil, pushing will fail and crash the app.
Click to reveal answer
Which method do you use to navigate to a new screen by adding it to the navigation stack?
ApopViewController(animated:)
Bpresent(_:animated:completion:)
CpushViewController(_:animated:)
Ddismiss(animated:completion:)
✗ Incorrect
pushViewController(_:animated:) adds a new view controller on top of the navigation stack.
What happens when you call present(_:animated:completion:)?
AIt pushes a new screen with a back button.
BIt shows a new screen modally over the current one.
CIt removes the current screen.
DIt reloads the current screen.
✗ Incorrect
present(_:animated:completion:) shows a new screen modally, covering the current screen.
Before pushing a view controller, you should check if:
AThe app is in background.
BThe view controller is nil.
CThe view controller is already visible.
DThe navigationController property is not nil.
✗ Incorrect
You must ensure navigationController is not nil to push safely.
Which navigation style provides a back button automatically?
APushing on navigation stack
BModal presentation
CPresenting a popover
DReplacing root view controller
✗ Incorrect
Pushing on a navigation stack adds a back button to return to the previous screen.
To close a modally presented view controller, you use:
Adismiss(animated:completion:)
BpushViewController(_:animated:)
CpopViewController(animated:)
Dpresent(_:animated:completion:)
✗ Incorrect
dismiss(animated:completion:) closes a modally presented view controller.
Explain how to navigate from one screen to another programmatically in iOS using Swift.
Think about the two main ways: pushing on navigation stack and presenting modally.
You got /4 concepts.
Describe the difference between pushing a view controller and presenting it modally.
Consider how the user returns to the previous screen in each case.
You got /4 concepts.
Practice
(1/5)
1. What does pushViewController do in iOS programmatic navigation?
easy
A. It moves to a new screen by adding it on top of the navigation stack.
B. It removes the current screen from the navigation stack.
C. It resets the entire navigation stack to the root screen.
D. It presents a screen modally without 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 method pushViewController adds 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 A
Quick Check:
pushViewController = move forward [OK]
Hint: Push adds a screen on top; pop removes the top screen [OK]
Common Mistakes:
Confusing push with pop behavior
Thinking push resets the stack
Mixing modal presentation with push navigation
2. Which of the following is the correct syntax to push a view controller named DetailViewController onto the navigation stack?
easy
A. navigationController.pushViewController(DetailViewController, animated: true)
B. navigationController?.pushViewController(DetailViewController(), animated: true)
C. navigationController.push(DetailViewController(), animated: true)
D. navigationController?.pushViewController(DetailViewController, animated: false)
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
Use DetailViewController() to create an instance before pushing.
Final Answer:
navigationController?.pushViewController(DetailViewController(), animated: true) -> Option B