0
0
iOS Swiftmobile~5 mins

Programmatic navigation in iOS Swift

Choose your learning style9 modes available
Introduction

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.

When you want to go to a new screen after a user action like a button tap.
When you need to move back to a previous screen automatically.
When you want to pass data to the next screen before showing it.
When navigation depends on some logic, like login success.
When you want to open a screen without using storyboard segues.
Syntax
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.

Examples
This pushes a new DetailViewController onto the navigation stack, showing the detail screen.
iOS Swift
let detailVC = DetailViewController()
navigationController?.pushViewController(detailVC, animated: true)
This goes back to the previous screen in the navigation stack.
iOS Swift
navigationController?.popViewController(animated: true)
Here, we create a ProfileViewController, set a user ID, then navigate to it.
iOS Swift
let profileVC = ProfileViewController()
profileVC.userId = 123
navigationController?.pushViewController(profileVC, animated: true)
Sample App

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.

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: 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
OutputSuccess
Important Notes

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.

Summary

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.