Để điều hướng từ View Controller này sang View Controller khác trong iOS, chúng ta cần sử dụng Navigation Controller. Navigation Controller quản lý một stack (ngăn xếp) các View Controller khi chúng ta đi từ view này sang view khác.
Các bước thực hiện
Bước 1: Tạo đối tượng View Controller đích
let vc = self.storyboard?.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController
Trong bước này, chúng ta khởi tạo đối tượng kiểu View Controller đích. Identifier phải trùng với Storyboard ID của View Controller thứ hai.
Bước 2: Điều hướng sang View Controller khác
Cách 1: Push (dùng với Navigation Controller)
self.navigationController?.pushViewController(vc, animated: true)
Đẩy View Controller mới lên đỉnh stack. Phù hợp cho luồng điều hướng phân cấp (drill-down).
Cách 2: Present (Modal)
self.present(vc, animated: true, completion: nil)
Hiển thị View Controller dưới dạng modal. Phù hợp cho các tác vụ tạm thời (login, settings, camera...).
Kết quả chạy trên thiết bị

Lưu ý quan trọng
- Đối với push: View Controller hiện tại phải đang nằm trong Navigation Controller (
self.navigationController != nil). - Đối với present: Có thể gọi từ bất kỳ View Controller nào.
- Để quay lại: Dùng
navigationController?.popViewController(animated: true)(push) hoặcdismiss(animated: true)(present).