0
0
iOS Swiftmobile~5 mins

Navigation path management in iOS Swift

Choose your learning style9 modes available
Introduction

Navigation path management helps users move between screens in an app smoothly. It keeps track of where you are and where you can go next.

When you want to open a new screen after a button tap.
When you want to go back to the previous screen.
When you want to jump to a specific screen deep inside the app.
When you want to keep a history of screens visited.
When you want to pass data between screens during navigation.
Syntax
iOS Swift
let navigationController = UINavigationController(rootViewController: firstViewController)
navigationController.pushViewController(secondViewController, animated: true)
navigationController.popViewController(animated: true)

Use pushViewController to go forward to a new screen.

Use popViewController to go back to the previous screen.

Examples
This moves the user to the detail screen with animation.
iOS Swift
navigationController.pushViewController(detailViewController, animated: true)
This returns the user to the previous screen with animation.
iOS Swift
navigationController.popViewController(animated: true)
This sends the user back to the first screen in the navigation stack.
iOS Swift
navigationController.popToRootViewController(animated: true)
Sample App

This app starts with a white screen and a button. When you tap the button, it moves to a gray screen with a "Go Back" button. Tapping "Go Back" returns to the first screen.

iOS Swift
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: 100, width: 200, 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: 100, width: 200, height: 50)
    view.addSubview(button)
  }

  @objc func goBack() {
    navigationController?.popViewController(animated: true)
  }
}

@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 firstVC = FirstViewController()
    let navController = UINavigationController(rootViewController: firstVC)
    window?.rootViewController = navController
    window?.makeKeyAndVisible()
    return true
  }
}
OutputSuccess
Important Notes

Always use a UINavigationController to manage navigation paths in iOS apps.

Navigation controllers keep a stack of screens to allow easy back and forth movement.

Remember to set the rootViewController of the navigation controller to your first screen.

Summary

Navigation path management controls moving between screens in an app.

Use push to go forward and pop to go back.

UINavigationController manages the stack of screens automatically.