我在 UICollectionViewCell
中有一个 UITableView
和 UITableViewCell
,它是在 ViewController
中创建的。
对于 UITableView
和 UITableViewCell
我有一个单独的子类文件。在 UITableViewCell
-Subclass 中,我有一个值更改委托(delegate)操作链接到其中的 TextField
。现在,每当 Textfield 的这个值发生变化(调用函数)时,我都需要将该值传递到我的 ViewController
进行处理。
关于 MVC 原则,这是如何完成的?
最佳答案
您可以使用 block 或委托(delegate)或将值传回 ViewController。
在 UITableViewCell
子类中声明一个委托(delegate)
protocol TableViewCellDelegate {
func new(value: Any) // your data type here
}
class MySubclass: UITableViewCell {
weak var delegate: TableViewCellDelegate?
}
现在在 ViewController
中连接到这个委托(delegate)。您可以将它传递给 UICollectionViewCell
子类,然后从那里传递给 UITableViewCell
在 UITextField
的值更改委托(delegate)中调用您的方法
self.delegate?.new(value: "NewTextHere!")
这样就可以将数据传回ViewController
关于swift - 从 UITableViewCell 发送数据到 Controller MVC-Principle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47237485/