Generally, the object that you pass to the delegate can be used so that the same delegate class can be used in different contexts. This gives you more flexibility in cases when a delegate class could potentially be reused.
For example, if showAndHideCommentView
needs to interact with a view being shown or hidden, it has two ways of doing it:
- Get the view as an argument, or
- Reference the view directly, knowing that this delegate is attached to a particular view.
Example of the first approach:
@implementation MyDelegate
-(void)showAndHideCommentView:(LZCommentView *)commentView {
if (isShowing) {
[commentView doSomething];
}
}
@end
Example of the second approach:
@implementation MyDelegate
-(void)showAndHideCommentView {
if (isShowing) {
[self.commentView doSomething];
}
}
@end
The first approach is more flexible than the second one, because it lets you reuse the same code.
According to Apple, it’s best practice to define your protocols to conform to the NSObject
protocol, so the answer to your second question is "yes".
As far as the third question goes, the best way to declare delegate properties is with the weak
attribute to avoid retain cycles.