import UIKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
title = "Welcome Screen"
let button = UIButton(type: .system)
button.setTitle("Go to Details", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(goToDetails), for: .touchUpInside)
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
@objc func goToDetails() {
let detailVC = DetailViewController()
navigationController?.pushViewController(detailVC, animated: true)
}
}
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
title = "Detail Screen"
// Back button appears automatically
}
}
@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 mainVC = MainViewController()
let navController = UINavigationController(rootViewController: mainVC)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
}This app uses a UINavigationController to manage the flow between screens. The main screen has a button labeled "Go to Details". When tapped, it pushes the detail screen onto the navigation stack, showing the new screen with a back button automatically provided by the navigation controller. This back button lets users return to the main screen. This structure helps organize app flow by stacking screens and allowing easy navigation back and forth, just like pages in a book.