Search code examples
swiftxcodeswift4

push ViewController without storyboard


I try to push a view controller from the main ViewController like this (Swift 4):

@objc func childAction(sender: UIButton!) {
    print("Child button tapped")
    let vc = childDetailViewController()
    self.navigationController?.pushViewController(vc, animated: true)
}

The text is printed, but the viewController is not pushed. What did I missed?


Solution

  • Inside AppDelegate's didFinishLaunchingWithOptions do

    let fir = FirstVC()
    self.window?.rootViewController = UINavigationController(rootViewController: fir)
    

    Then this

    self.navigationController?.pushViewController(vc, animated: true)
    

    should work