Understanding the multiple references to the delegate protocol in sample code

I got this code from one of Apple's examples:

@protocol SectionHeaderViewDelegate;


@interface SectionHeaderView : UIView {
}

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIButton *disclosureButton;
@property (nonatomic, assign) NSInteger section;
@property (nonatomic, assign) id <SectionHeaderViewDelegate> delegate;

-(id)initWithFrame:(CGRect)frame title:(NSString*)title section:(NSInteger)sectionNumber delegate:(id <SectionHeaderViewDelegate>)aDelegate;
-(void)toggleOpenWithUserAction:(BOOL)userAction;

@end



/*
 Protocol to be adopted by the section header delegate; the section header tells its delegate when the section should be opened and closed.
 */
@protocol SectionHeaderViewDelegate <NSObject>

@optional
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section;
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section;

@end

I am confused by some notation. This is my attempt to explain this. Please correct me if I am wrong:

The first @protocol SectionHeaderViewDelegate;announces the start of the protocol for the class SectionHeaderView. A fourth property is id <SectionHeaderViewDelegate> delegate;required for classes that conform to the protocol so that they can do something like instanceOfClass.delegate = self;.

Then after /* comment */I'm not sure why the protocol directive is used again. Is it part of the same protocol? Is this different from the protocol announced in the first half?

Is my explanation and understanding of the code correct?

+3
source share
3

protocol - , . , , , @protocol SectionHeaderViewDelegate; , , , . , id<SectionHeaderViewDelegate> delegate SectionHeaderView. @protocol .

+6

@protocol . @protocol SuchAndSuch; - - " , , ".

, , . @protocol SuchAndSuch … @end.

0

The first @protocol SectionHeaderViewDelegate tells the class that there is a protocol with this name (with avoir compilation error)

The fourth property, delegate, means that there is an attribute named "delegate", which can be of any class (id) and which implements the protocol "SectionHeaderViewDelegate"

And at the end of the file, the protocol is determined using imeplent methods

0
source

All Articles