简体   繁体   English

从呈现视图控制器推送视图控制器

[英]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. 在所呈现的表视图控制器的didSelectRowAt方法中,我实例化另一个视图控制器。 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. 但是工人不会告诉经理该做什么 - 这不是工人的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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