0
0
iOS Swiftmobile~20 mins

Programmatic navigation in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Navigation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when this SwiftUI button is tapped?
Consider this SwiftUI code snippet. What is the visible result when the button is tapped?
iOS Swift
struct ContentView: View {
  @State private var isDetailActive = false
  var body: some View {
    NavigationView {
      VStack {
        NavigationLink(destination: Text("Detail View"), isActive: $isDetailActive) {
          EmptyView()
        }
        Button("Go to Detail") {
          isDetailActive = true
        }
      }
    }
  }
}
AThe app navigates to a new screen showing "Detail View" text.
BNothing happens because NavigationLink is hidden.
CThe button causes a crash due to missing NavigationLink label.
DThe button toggles a boolean but does not navigate.
Attempts:
2 left
💡 Hint
Think about how NavigationLink with isActive binding works in SwiftUI.
lifecycle
intermediate
2:00remaining
What is the effect of calling pushViewController in UIKit?
In UIKit, what happens when you call navigationController?.pushViewController(newVC, animated: true)?
AThe newVC is presented modally over the current view.
BThe current view controller is replaced without animation.
CThe newVC is added on top of the navigation stack and shown with animation.
DThe app crashes because pushViewController requires a storyboard.
Attempts:
2 left
💡 Hint
Think about how navigation stacks work in UIKit.
📝 Syntax
advanced
2:00remaining
Which Swift code correctly performs programmatic navigation in SwiftUI?
Select the code snippet that correctly navigates to DetailView when a button is pressed.
iOS Swift
struct ContentView: View {
  @State private var showDetail = false
  var body: some View {
    NavigationView {
      VStack {
        Button("Show Detail") {
          showDetail = true
        }
        // NavigationLink here
      }
    }
  }
}
ANavigationLink(destination: DetailView(), isActive: showDetail) { Text("Go") }
BNavigationLink(destination: DetailView(), isActive: $showDetail) { EmptyView() }
CNavigationLink(isActive: showDetail, destination: DetailView()) { EmptyView() }
DNavigationLink(destination: DetailView()) { Button("Show Detail") { showDetail = true } }
Attempts:
2 left
💡 Hint
Check the correct use of isActive binding in NavigationLink.
🔧 Debug
advanced
2:00remaining
Why does this UIKit navigation code fail to show the new screen?
Given this code snippet, why does tapping the button not navigate to NewViewController? class ViewController: UIViewController { @IBAction func buttonTapped(_ sender: UIButton) { let newVC = NewViewController() newVC.modalPresentationStyle = .fullScreen } }
ABecause modalPresentationStyle must be set after presenting.
BBecause buttonTapped is missing @objc attribute.
CBecause NewViewController must be loaded from storyboard.
DBecause newVC is created but never pushed or presented.
Attempts:
2 left
💡 Hint
Check if the new view controller is actually shown.
🧠 Conceptual
expert
2:00remaining
What is the main difference between programmatic navigation and declarative navigation in iOS?
Choose the best explanation of the difference between programmatic navigation (UIKit) and declarative navigation (SwiftUI).
AProgrammatic navigation uses explicit commands to push or present views, while declarative navigation uses state bindings to describe navigation paths.
BProgrammatic navigation requires storyboards, declarative navigation does not.
CDeclarative navigation is only for modal views, programmatic is for push navigation.
DProgrammatic navigation is faster at runtime than declarative navigation.
Attempts:
2 left
💡 Hint
Think about how navigation is triggered in UIKit vs SwiftUI.