1
votes

I have a view controller that presents a table view controller modally. In the didSelectRowAt method of the presented table view controller I instantiate another view controller. I want to dismiss the presented view controller and have the presenting view controller push this new view controller.

Can I use this in some way?

self.presentingViewController?.navigationController?.pushViewController(newVC, animated: true)
3
So you want to dismiss the modal view controller and push a view controller on that same modal view controller? I think I'm missing something here because this does not make sense (as it's non-standard iOS behaviour and thus confusing to users).meaning-matters
I want to push a view controller from the view that presents the modal view when a selection is made in the modal viewraginggoat
What about didSelectRowAt? So the modal table view controller first appears, then the user taps a cell and then you want to present something from the non-modal parent view controller???meaning-matters
do you want to dismiss your tableViewController too?Umair Aamir
@UmairAamir Yesraginggoat

3 Answers

3
votes

I want to dismiss the presented view controller and have the presenting view controller push this new view controller.

A modal view controller usually represents some sort of question, like What photo do you want to see? or Which contact do you want to talk to? The modal view controller should let the user indicate an answer, and then it should return that answer to its parent, the presenting controller. That presenting view controller is the one that should be in charge of what to do next. Dismiss the modal controller? Adjust the data model? Push a new controller onto the navigation stack?

Think of the relationship between the presenting view controller and the modal controller as an employment agreement, where the presenting controller is a manager and the modal controller is a worker. The worker might do some task at the manager's request and then report back, so that the manager can decide what to do next. But a worker wouldn't tell the manager what to do -- that's not the worker's job.

1
votes

No , it will not work use delegate to inform the presenting view Controller to dismiss the modal and do the push like this

  self.dismiss(animated: false, completion: nil)

  let vc = self.storyboard?.instantiateViewController(withIdentifier: "identifier")

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

You will have to setup a delegate, or use a different way to tell the view controller's parent to dismiss it and present the new view controller.