Why do we need to set delegate to self? Why isn't it defaulted by the compiler?

前端 未结 3 1161
野趣味
野趣味 2021-01-17 08:13

I think I fully understand the concept of delegation, my question is that when we do:

class someViewController : UIViewController, UITableViewDelega         


        
3条回答
  •  日久生厌
    2021-01-17 08:53

    would it ever be possible that we wouldn't want to set tableView.delegate to self?

    What's tableView.delegate? Your class someViewController is not a subclass of UITableViewController (and does not have a tableView property). It does not even know that you handle a table view.

    The fact that your type declaration conforms to UITableViewDelegate does not make it obvious on which actual table view instance it should be set.

    If there is a chance that tableView.delegate is set to something other than self...well what is that?

    Often times it's wise to split up functionality into multiple types to reduce the complexity of the view controller. Your someViewController might have a viewModel property that handles all thing regarding the table view.

提交回复
热议问题