Search code examples
iosswiftuinavigationcontrollerpushviewcontroller

PUSH to ViewController from other viewcontroller which is presented as popover - WITHOUT STORY BOARD


I am NOT USING StoryBoard in my project. I have presented one view controller as popOver Style.

let vcOne                               = ViewControllerOne()
    vcOne.showCorrectionFactor          = true
    vcOne.modalPresentationStyle        = .popover
    let pop                             = vcOne.popoverPresentationController
    pop?.sourceView                     = self.view
    pop?.sourceRect                     = sender.frame
    vcOne.preferredContentSize          = CGSize(width: 400, height: 500)
    self.present(vcOne, animated: true, completion: nil)

Now i want to navigate to other view controller from ViewControllerOne() and with in this pop up.

I am doing it like

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

But self.navigationController? is getting nil value. How can i achieve this without story board.


Solution

  • You did not embed ViewControllerOne in a UINavigationController, so of course self.navigationController is nil.

    Just do this:

    // embed it!
    let navController                   = UINavigationController(rootViewController: vcOne)
    // get the popover presentation controller from the navigation controller!
    let pop                             = navController.popoverPresentationController
    pop?.sourceView                     = self.view
    pop?.sourceRect                     = sender.frame
    vcOne.preferredContentSize          = CGSize(width: 400, height: 500)
    // present navController rather than vcOne!
    self.present(navController, animated: true, completion: nil)