Delegates, can't get my head around them

后端 未结 4 832
长情又很酷
长情又很酷 2020-12-14 13:09

Hey, I\'m looking for useful resources about Delegates. I understand that the delegate sits in the background and receives messages when certain things happen - e.g. a table

4条回答
  •  执念已碎
    2020-12-14 14:01

    In Cocoa, objects almost always identify themselves when calling a delegate method. For example, UITableView passes itself as the first parameter of the delegate message when calling it:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    If you wanted the same delegate to handle multiple UITableViews, then you just need a some conditional on the tableView object passed to the method:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == self.myFirstTableView) {
            // do stuff
        } else if (tableView == self.mySecondtableView) {
            // do other stuff
        }
    }
    

    }

    If you don't want to compare the object pointers directly, you can always use the tag property to uniquely identify your views.

提交回复
热议问题