简体   繁体   English

如何从UItableviewcell呈现viewcontroller

[英]How to Present a viewcontroller from a UItableviewcell

I am trying to follow the MVC model in developing my iOS app and have an issue here: how do I fire a viewcontroller from a tableviewcell, instead of its parent tableviewcontroller. 我试图在开发我的iOS应用程序时遵循MVC模型并在此处遇到问题:如何从tableviewcell而不是其父tableviewcontroller触发viewcontroller。 PresentViewController can only be invoked from a viewcontroller. 只能从viewcontroller调用PresentViewController。

Here is the situation: 情况如下:

  • I have a data object at the backend myDataObject; 我在后端myDataObject有一个数据对象;

  • I also have a tableview where I populated a type of table view cell, myDataCell, with an instance of MyDataObject; 我还有一个tableview,我在其中填充了一个表格视图单元格myDataCell,其中包含一个MyDataObject实例;

  • Now, I would like to programmatically (instead of segue) trigger another ViewController from a UIButton click event in myDataCell. 现在,我想以编程方式(而不是segue)从myDataCell中的UIButton click事件触发另一个ViewController。

My goal is to decouple the MVC as much as possible, by trying to avoid having each object keeping pointers to the other two involved, for example, I do not want the cell to keep reference of MyDataObject, nor do I want it to keep reference of its tableviewcontroller. 我的目标是尽可能地解耦MVC,试图避免让每个对象保持指向其他两个参与的指针,例如,我不希望单元格保留MyDataObject的引用,也不希望它保留引用其tableviewcontroller。

Here are several scenarios of how to handle this situation, and the challenges: 以下是如何处理这种情况的几种情况以及挑战:

  1. declare myDataCellDelegate and myDataObjectDelegate, implement methods from both delegate declarations in the tableviewcontroller, and set a reference of the tableviewcontroller and myDataCell in the delegate. 声明myDataCellDelegate和myDataObjectDelegate,从tableviewcontroller中的委托声明实现方法,并在委托中设置tableviewcontroller和myDataCell的引用。 This way myDataObject has a reference to the tableviewcontroller and myDataCell. 这样myDataObject就引用了tableviewcontroller和myDataCell。

Question here is how do I invoke handleButtonTouched event from myDataCell, and still be able to present the next viewcontroller, without accessing myDataObject from myDataCell? 这里的问题是如何从myDataCell调用handleButtonTouched事件,并且仍然能够呈现下一个viewcontroller,而无需从myDataCell访问myDataObject? Or do I have to keep a reference of myDataObject in myDataCell? 或者我是否必须在myDataCell中保留myDataObject的引用? I already have a reference of myDataCell in myDataObject from the delegate method above. 我已经从上面的委托方法在myDataObject中引用了myDataCell。

  1. declare myDataObjectDelegate only, and implement the delegate methods in the tableviewcontroller. 仅声明myDataObjectDelegate,并在tableviewcontroller中实现委托方法。 I can set the reference of the tableviewcontroller and myDataCell in the delegate. 我可以在委托中设置tableviewcontroller和myDataCell的引用。 This way myDataCell does not have to have reference to its parent viewcontroller, only myDataObject has reference to both. 这样myDataCell就不必引用它的父视图控制器,只有myDataObject引用了它们。

With this approach, I can do myDataObject->tableviewcontroller->presentviewcontroller. 通过这种方法,我可以执行myDataObject-> tableviewcontroller-> presentviewcontroller。 But how do I make the myDataCell->buttonclick event as the trigger? 但是如何将myDataCell-> buttonclick事件作为触发器? I still need to pass the info from myDataObject to myDataCell. 我仍然需要将信息从myDataObject传递给myDataCell。

Thanks guys. 多谢你们。

EDIT: 编辑:

I forgot to mention that my goal of going through such trouble is that I intend to keep myDataCell and myDataObject in a framework. 我忘了提到我经历这样麻烦的目的是我打算将myDataCell和myDataObject保存在一个框架中。

Why are you opposed to using the normal UITableViewDelegate & UITableViewDataSource methods? 你为什么反对使用普通的UITableViewDelegateUITableViewDataSource方法? They do an excellent job of adhering to MVC. 他们在坚持MVC方面表现出色。

I always have a view controller that acts as the delegate and data source for the table view. 我总是有一个视图控制器,它充当表视图的委托和数据源。 In the view controller I make a request to a data manager (essentially a controller that handles interaction with the model layer) that will return some collection of data. 在视图控制器中,我向数据管理器(本质上是处理与模型层的交互的控制器)发出请求,该请求将返回一些数据集合。 I keep that data (typically an NSArray ) as a property in the view controller and reload the table view. 我将该数据(通常是NSArray )保存为视图控制器中的属性并重新加载表视图。

I then implement -tableView:didSelectRowAtIndexPath: . 然后我实现-tableView:didSelectRowAtIndexPath: . There I can see which cell was selected, which data object is associated with that cell (without having the cell itself keep a reference to the data), and handle any transition I need to at that point. 在那里,我可以看到选择了哪个单元格,哪个数据对象与该单元格相关联(没有单元格本身保留对数据的引用),并处理此时需要的任何转换。 This follows MVC because the view doesn't know anything about the data. 这遵循MVC,因为视图对数据一无所知。

I think you are over-analysing a little. 我觉得你过分分析了一下。 Check out this Apple document which explains the MVC design pattern. 查看这份解释MVC设计模式的Apple文档 Key is that the views should not reference the model, nor vice versa. 关键是视图不应该引用模型,反之亦然。 The controller acts as the go-between. 控制器充当中间人。

Subclassing UITableViewCell is a way to follow MVC. 子类化UITableViewCell是一种遵循MVC的方法。

I'm going to explain my approach of following MVC if i have a scenario like above. 如果我有一个类似上面的场景,我将解释我跟随MVC的方法。

Approach: 做法:

I figure out two problems from above explanations - 我从上面的解释中找出了两个问题 -

  1. Where to define buttonTapped events 在哪里定义buttonTapped事件

  2. Maintain TableViewController object in cell 在单元格中维护TableViewController对象

Correct me if i'm misguide or missing something. 如果我误导或遗漏某些东西,请纠正我。

First thing - handleButtonTouched: 第一件事 - handleButtonTouched:

Create button object in .h file of your custom cell, and add it's target method in cellForRowAtIndexPath of your TableViewController , So that you can get your selector from TableViewController itself. 在自定义单元格的.h文件中创建按钮对象,并在TableViewController cellForRowAtIndexPath中添加它的目标方法,这样就可以从TableViewController本身获取选择器。

Second thing - How to fire presentViewController event from cell 第二件事 - 如何从单元格中触发presentViewController事件

Create one more variable of UIViewController assign it in cellForRowAtIndexPath method. 再创建一个UIViewController变量,在cellForRowAtIndexPath方法中赋值。 Now from cell you can present your viewController using this object. 现在,从单元格中,您可以使用此对象呈现viewController。

Give a comment if not clear. 如果不清楚,发表评论。

Update: 更新:

  • implemented a manager to link the view controller and myDataObject; 实现了一个管理器来链接视图控制器和myDataObject;

  • added gesture recognizer on myCell 在myCell上添加了手势识别器

A cell is a view in the MVC. 单元格是MVC中的视图。 The controller must do the logic, it's the intermediate. 控制器必须做逻辑,它是中间件。 In that case I always created a custom delegate, something like UItableviewcellDelegate with something like showThisVC:(UITableViewCell)cell and you can get the row number easily because you know the cell in the controller. 在这种情况下,我总是创建一个自定义委托,类似于UItableviewcellDelegate,类似于showThisVC:(UITableViewCell)单元格,您可以轻松获取行号,因为您知道控制器中的单元格。

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

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