rantanplan
rantanplan

Reputation: 244

First present view controller and then push child view controllers

Is there a trick to push to child view controllers from a parent vc that previously has been presented modally?

Method to present parent that I'm using:

    let parentVC = ParentController()
    self.present(parentVC, animated: true, completion: nil)

Method to then push to child controllers which doesn't work:

    let childVC = childController()
    navigationController?.pushViewController(childVC, animated: true)

Upvotes: 0

Views: 1655

Answers (1)

Calvin
Calvin

Reputation: 599

Is there a trick to push to child view controllers from a parent vc that previously has been presented modally?

If you present a vc modally and you want this vc to push a child vc, you have to present the vc embedded in a UINavigationController.

    let parentVC = ParentController()
    self.present(parentVC, animated: true, completion: nil)

to become

    let parentVC = ParentController()
    let parentNav = UINavigationController(rootViewController: parentVC)
    self.present(parentNav, animated: true, completion: nil)

then you can do, in parentVC:

    let childVC = childController()
    navigationController?.pushViewController(childVC, animated: true)

Upvotes: 1

Related Questions