lundi 2 mars 2020

What is a good way to make references between each UIView in the hierarchy?

Suppose you have a UIView hierarchy like this

UIViewController
->UITableView
-->UITableViewCell
--->UITextView

Now when an event happens say

func textViewDidEndEditing(_ textView: UITextView) {}

You have a reference to the textView, but lets say you need an UI to change for the entire hierarchy and also data models, how do you do that?

One approach is to give it all the references, but it's very tedious

class CustomTextView {
  init(model:Data, cell:UITableViewCell, tableview:UITableView) {//...}
  func textViewDidEndEditing(_ textView: UITextView) {
     //update model, update cell, update tableview
  }
}

Also I would need to pass similar references to the cell and the tableview.

Another approach is to set the delegate at the controller level. And then you have:

class myController:UITableViewController {
  func textViewDidEndEditing(_ textView: UITextView) {
    //tableView.updateUI()
    //cell = tableView.cellForRowAtIndexPath...
    //cell.updateUI()
    //cell.textView.updateUI()
    //model.updateData()
  }
}

But then this leads to a massive view controller.

What is the right design pattern here where every view needs access to every other view?

Aucun commentaire:

Enregistrer un commentaire