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

前端 未结 2 521
温柔的废话
温柔的废话 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
    
    0 讨论(0)
  • 2020-12-28 18:13
    var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")
    
    var aObjNavi = UINavigationController(rootViewController: objVC)
    

    Now, instead of using view controller object use navigation controller object.

    0 讨论(0)
提交回复
热议问题