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.
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
}
}