I am new to objective C. I want to ask why “delegate” is needed in objective C. Could you provide me specific reasons, situations and code examples for this. I appreciate if anyone could help.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This idiom is how what we might call “callbacks” in other languages are typically done in ObjC. A class needs to react to a given situation (let’s say a mouse click). The class is rather generic, and not coded with any domain-specific knowledge, so the policy for that action isn’t written by that class. Hence it needs to “delegate” that action to your code. So it calls a method on an object you specify. Since your code presumably knows what it wants to do when you click the mouse.
Apparently there’s even a Wikipedia article on this pattern, written by the kind of people who like to give special names to patterns. See: Delegation pattern.