Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 理解示例代码中多次提到的委托协议_Objective C_Ios_Cocoa Touch_Delegates_Protocols - Fatal编程技术网

Objective c 理解示例代码中多次提到的委托协议

Objective c 理解示例代码中多次提到的委托协议,objective-c,ios,cocoa-touch,delegates,protocols,Objective C,Ios,Cocoa Touch,Delegates,Protocols,我从苹果的一个例子中得到了以下代码: @protocol SectionHeaderViewDelegate; @interface SectionHeaderView : UIView { } @property (nonatomic, retain) UILabel *titleLabel; @property (nonatomic, retain) UIButton *disclosureButton; @property (nonatomic, assign) NSInteger

我从苹果的一个例子中得到了以下代码:

@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's 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
@协议部分headerviewdelegate;
@接口部分HeaderView:UIView{
}
@属性(非原子,保留)UILabel*标题标签;
@属性(非原子,保留)UIButton*披露按钮;
@属性(非原子,赋值)NSInteger部分;
@属性(非原子,赋值)id委托;
-(id)initWithFrame:(CGRect)框架标题:(NSString*)标题节:(NSInteger)节号委托:(id)代理;
-(void)切换OpenWithUserAction:(BOOL)userAction;
@结束
/*
科长代表采用的协议;节头告诉其委托何时打开和关闭节。
*/
@协议部分headerviewdelegate
@可选的
-(无效)sectionHeaderView:(sectionHeaderView*)sectionHeaderView SectionOpen:(NSInteger)节;
-(无效)sectionHeaderView:(sectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section;
@结束
我对一些符号感到困惑。这是我试图解释的如果我错了,请纠正我:

第一个
@协议部分headerviewdelegate
声明
SectionHeaderView
类的协议开始。第四个属性,
id委托,以便它们可以执行类似于
instanceOfClass.delegate=self的操作

然后在
/*注释*/
之后,我不确定为什么会再次使用协议指令。它是同一协议的一部分吗?它与上半年宣布的协议不同吗


我对代码的上述解释和理解是否正确?

实际上,第一个
协议
声明是解决鸡和蛋问题的前向声明。delegator类和委托协议都需要相互了解,因此为了解决这个问题,我们声明
@protocol SectionHeaderViewDelegate作为一个转发声明,表示它尚未定义,但它将在那里,您不必担心它。当您在
部分HeaderView
类中执行
id委托时,此操作有效。下一个
@protocol
声明表示协议定义的实际开始。

第一个
@protocol
是协议的前向声明。它的形式是
@protocol,比如handsuch-注意分号-它只是告诉编译器“有一个协议使用这个名称,所以允许在任何需要协议的地方使用这个名称”


注释之后的第二次是协议实际定义的时间。此时,它的形式是
@protocol,例如handsuch…@end

第一个@protocol部分headerviewdelegate告诉类有一个具有此名称的协议(以避免编译错误)

第四个属性delegate表示有一个名为“delegate”的属性,该属性可以是任何类(id),并实现协议“SectionHeaderServiceWDelegate”

在文件的最后,用imeplent的方法定义了协议