0
0
iOS Swiftmobile~5 mins

Why navigation structures app flow in iOS Swift

Choose your learning style9 modes available
Introduction

Navigation helps users move through different screens in an app easily. It keeps the app organized and simple to use.

When your app has multiple screens or pages.
When users need to go back and forth between different parts of the app.
When you want to show a clear path or order for users to follow.
When you want to group related content or features together.
When you want to improve user experience by making the app flow logical.
Syntax
iOS Swift
let navigationController = UINavigationController(rootViewController: firstViewController)
navigationController.pushViewController(secondViewController, animated: true)
navigationController.popViewController(animated: true)
UINavigationController manages a stack of view controllers to handle navigation.
You push new screens onto the stack and pop them to go back.
Examples
This creates a navigation controller starting with the Home screen.
iOS Swift
let navController = UINavigationController(rootViewController: HomeViewController())
This moves to the Detail screen with animation.
iOS Swift
navController.pushViewController(DetailViewController(), animated: true)
This goes back to the previous screen with animation.
iOS Swift
navController.popViewController(animated: true)
Sample App

This app starts with a Home screen that has a button. When you tap the button, it navigates to a Details screen. You can go back using the navigation bar.

iOS Swift
import UIKit

class HomeViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    title = "Home"

    let button = UIButton(type: .system)
    button.setTitle("Go to Details", for: .normal)
    button.addTarget(self, action: #selector(goToDetails), for: .touchUpInside)
    button.frame = CGRect(x: 100, y: 200, width: 150, height: 50)
    view.addSubview(button)
  }

  @objc func goToDetails() {
    let detailVC = DetailViewController()
    navigationController?.pushViewController(detailVC, animated: true)
  }
}

class DetailViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .lightGray
    title = "Details"
  }
}

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let homeVC = HomeViewController()
    let navController = UINavigationController(rootViewController: homeVC)
    window?.rootViewController = navController
    window?.makeKeyAndVisible()
    return true
  }
}
OutputSuccess
Important Notes

Navigation controllers keep track of screens in a stack, like a stack of cards.

Users expect to use the back button to return to the previous screen.

Always set clear titles for each screen to help users know where they are.

Summary

Navigation structures the app flow by managing screen transitions.

It helps users move easily between different parts of the app.

Using UINavigationController is the standard way to handle navigation in iOS apps.