将值返回给父视图
我遇到以下情况:我的表格单元格上连接了一个 UISwipeGestureRecognizer。滑动时,两个按钮的 .hidden-status 切换为 NO
。
这些细胞是定制的。意思是,这些按钮的 IBAction 位于该控制器中。现在我希望这些自定义单元格中的 UIButton 将变量发送回父视图。该父视图是 UIViewController(包含 UITableView)。
我该怎么做?我读过有关协议的内容,但我找不到可靠的例子......或者还有其他想法吗?我们都欢迎他们:)
I've got the following situation: I've got a UISwipeGestureRecognizer hooked up on my table cell. On swipe, two buttons get their .hidden-status switched to NO
.
Those cells are custom made. Meaning, the IBAction of those buttons is in that controller. Now I'd like the UIButton in those custom cells to send back a variable to the parent view. That parent view is the UIViewController (containing a UITableView).
How can I do this? I've read about protocols, but I can't get a solid example for that... Or are there any other ideas? They are all welcome :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遵循的方法可能会导致崩溃或内存泄漏,因为您必须保留 uitableview 单元格内容的控制器。
然而,协议是一种简单的方法,在创建表视图单元格后,您可以将视图控制器(创建表视图)分配为单元格内容视图的所有操作的委托。
在您的自定义单元格的控制器中,只需调用委托方法,它将由您的父视图处理。
这听起来可能令人困惑,但其实很简单。将尝试发布示例代码。
在表视图单元格视图控制器中完成上述操作后,在主控制器中创建视图控制器的实例,然后
在单元格的视图控制器中,当用户点击或执行任何操作时调用
或
这样您的主控制器就会得到该事件并且可以被处理。
另一种方法是在创建单元格并处理事件时创建按钮。我会推荐这种方法。
上面的行是从此处的答案复制的。
The approach you are following may result in a crash or a memory leak as you have to retain the controller of your uitableview cell content.
However protocols are simple approach where after you create your table view cell you can allocate the viewcontroller (which creates the table view) as a delegate to all actions for the cell's content view.
In your custom cell's controller just call the delegate method and it will be handled by your parent view.
It may sound confusing, but its really simple. Will try to post sample code.
Once you are done with the above in your table view cell view controller, in your main controller create an instance of your view controller and then
In your cell's view controller when a user taps or performs any action then call
or
This way your main controller gets the event and it can be processed.
Alternate way to do this would be to create the buttons when you create the cell and handle the events. I would recommend this approach.
The above line is copied from an answer here.