Question

I have used @protocols many times but I think I have been doing it wrong all the time. They have worked always well, but now I want to improve my level so I am trying to do it the better I can.

I always have created a delegate like this:

@protocol CommentViewDelegate;

@interface LZCommentView : UIView
@property (assign, nonatomic) id  <CommentViewDelegate> delegate;
@end

@protocol CommentViewDelegate
-(void)showAndHideCommentView;
@end

Now, I have seen that almost all the delegate methods that I see send their own object. Something like this:

 -(void)showAndHideCommentView:(LZCommentView *)commentView;

What is the difference between what I did and this? Is one of them better than the other? I have seen that almost everyone who does this, does not use the object in the ViewController.

Another question is, should I use <NSObject> in the @protocol definition?

And the last one, what is better create the @property with assign or strong?

Thank you

Was it helpful?

Solution

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:

  1. Get the view as an argument, or
  2. 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.

OTHER TIPS

1) You should always make your protocol conform to the NSObject protocol. This lets you make use of all of the methods in that protocol.

@protocol CommentViewDelegate <NSObject>

2) Unless you have a good reason to do otherwise, most properties for delegates should be defined as weak. This avoids reference cycles and it ensure the delegate is automatically set to nil if the delegate object is deallocated.

@property (nonatomic, weak) id<CommentViewDelegate> delegate;

3) It's best to include the object in the protocol methods because it offers the most flexibility. It also allows a class to be the delegate of more than one instance. Then the class can tell which instance the protocol method is being called for. Think of a view controller handling multiple buttons or having two or more table views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top