How to add a navigation controller programmatically in code but not as initial view controller

前端 未结 2 527
温柔的废话
温柔的废话 2020-12-28 17:22

I am relatively new to swift and iOS and can\'t find an answer or any help for my problem that works.

I want to create a navigation controller view when I click on a

2条回答
  •  梦谈多话
    2020-12-28 18:01

    You can add UINavigationController like below :

    First you have to create object of SecondViewController,

    let objVC: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
    

    Or

    let objVC: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
    

    Or

    let objVC: SecondViewController? = SecondViewController()
    

    Then add Navigation to SecondViewController

    let aObjNavi = UINavigationController(rootViewController: objVC!)
    

    If you want to present then use :

    self.present(aObjNavi, animated: true) { 
    }
    

    If you want to push then use :

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

    If you want to set as root controller then use :

    let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
    appDelegate.window?.rootViewController = aObjNavi
    

提交回复
热议问题