I created simple project with two ViewControllers (ViewController and SecondVC) programmatically.
In ViewController I've added a button, which push to SecondVC.
In AppDelegate didFinishLaunchingWithOptions
I've set ViewController as rootViewController.
ViewController code:
class ViewController: UIViewController {
let button = UIButton.init(frame: CGRect(x: 30, y: 30, width: 200, height: 200))
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .brown
view.addSubview(button)
button.backgroundColor = .black
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
}
@objc func tapped() {
let vc = SecondVC()
self.navigationController?.pushViewController(vc, animated: true)
}
}
SecondVC code:
class SecondVC: UIViewController {
override func viewDidLoad() {
view.backgroundColor = .white
}
}
AppDelegate code:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
let mainVC = ViewController()
navigationController = UINavigationController(rootViewController: mainVC)
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
return true
}
In targets deployment info I've removed Main Interface:
When app starts, there is no navigation in my ViewController, so when I tap on button, navigationController
is nil, and nothing happens. Cannot find the problem the second day, please help to understand where the problem is.
I use XCode 11.3
sources like these, doesn't help me Creating a navigationController programmatically (Swift)