jeanggi90
jeanggi90

Reputation: 761

Send data from TableViewCell to Controller MVC-Principle

I have got a UITableView with UITableViewCell inside UICollectionViewCell which is created in the ViewController.

For both, the UITableView and the UITableViewCell I have a separate subclass file. In the UITableViewCell-Subclass I have a value changed delegate action linked to the TextField in it. Now whenever this value of the Textfield gets changed (the function is called) I need to pass the value down to my ViewController where it is processed.

How is that done regarding the MVC-principle?

Upvotes: 0

Views: 48

Answers (1)

carbonr
carbonr

Reputation: 6067

You can use blocks or delegates or pass the value back to the ViewController.

Inside the UITableViewCell subclass declare a delegate

protocol TableViewCellDelegate {
  func new(value: Any) // your data type here
}

class MySubclass: UITableViewCell {
  weak var delegate: TableViewCellDelegate?
}

Now in the ViewController connect to this delegate. You can pass it to the UICollectionViewCell subclass and from there to the UITableViewCell

Inside the value change delegate of the UITextField you call your method

   self.delegate?.new(value: "NewTextHere!")

this way the data can be passed back to the ViewController

Upvotes: 1

Related Questions