简体   繁体   中英

Push View Controller from Presenting View Controller

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)

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)

您必须设置委托,或使用不同的方式告诉视图控制器的父级将其解除并显示新的视图控制器。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM