iOS开发之protocol和delegate

 protocol——协议

协议是用来定义对象的属性,行为和用于回调的。
    协议中有两个关键字@private和@optional,@private表示使用这个协议必须要写的方法,@optional表示可选的方法,用不到可以不写。
就像UITableView,它有两个协议:UITableViewDelegate和UITableViewDataSource,UITableViewDelegate是规定行为操作的,里面的方法都是可选的方法,UITableViewDataSource是规定数据存储结构的,有两个必选方法。因此你再实现UITableViewDataSource时必须要定义这两个方法,否则程序会出错。
如果你使用了响应的类或者控件,如果该类定义了协议,就可以根据需要实现该协议。


<span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;">protocol和java里interface的概念类似,是Objective-C语法的一部分。</span><br style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;" /><span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;">定义protocol如下</span>
<span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;"></span><div class="dp-highlighter bg_cpp" style="font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 12px; width: 936.531px; overflow-x: auto; overflow-y: hidden; padding-top: 1px; position: relative; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 494px; top: 905px; width: 29px; height: 15px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@protocol ClassADelegate  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">- (<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)methodA;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">- (<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)methodB;  </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">@end  </span></li></ol></div>
<p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">那么就是定义了一组函数,这组函数放在一起叫作一个protocol,也就是协议。</p><p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">函数是需要被实现的,所以如果对于class如下</p><div class="dp-highlighter bg_cpp" style="font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 12px; width: 936.531px; overflow-x: auto; overflow-y: hidden; padding-top: 1px; position: relative; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 494px; top: 1182px; width: 29px; height: 15px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@interface ClassB <ClassADelegate> {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">@end  </span></li></ol></div>
<p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">就叫作ClassB conform to protocol ClassADelegate,也就是说ClassB实现了这个协议,
也就是实现了这一组函数。</p><p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">有了上面这个头文件,我们就可以放心作调用</p><div class="dp-highlighter bg_cpp" style="font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 12px; width: 936.531px; overflow-x: auto; overflow-y: hidden; padding-top: 1px; position: relative; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 494px; top: 1429px; width: 29px; height: 15px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">ClassB *b = [[ClassB alloc] init];  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">[b methodA];  </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">[b methodB];  </span></li></ol></div>
<p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">而不用担心出现unrecognized selector sent to instance这种错误了。</p><p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">所以protocol就是一组函数定义,是从类声明中剥离出来的一组定义。</p><div class="dp-highlighter bg_cpp" style="font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 12px; width: 936.531px; overflow-x: auto; overflow-y: hidden; padding-top: 1px; position: relative; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 494px; top: 1651px; width: 29px; height: 15px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">id<ClassADelegate> b = ...;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">[b methodA];  </span></li></ol></div>
<p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">这种用法也常见,b是一个id类型,它知道ClassADelegate这组函数的实现。</p><p style="margin-top: 0px; margin-bottom: 1.625em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; outline: 0px; vertical-align: baseline; color: rgb(55, 55, 55);">
</p>
<span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;"><strong><span style="font-size: 18px;">delegate——委托,代理</span></strong>
</span>
<span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;"><strong><span style="font-size: 18px;">
</span></strong></span>
<span style="font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: rgb(55, 55, 55);"><span style="font-size: 15px;">适用场合:</span></span><strong><span style="white-space: pre;"></span></strong><pre class="reply-text mb10" id="content-903091930" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: Arial; zoom: 1; display: inline !important; background-color: rgb(255, 255, 255);"><span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;">类A调用类B, 当类B想回调类A的方法的时候, 要用到delegate</span>
 
<span style="color: rgb(55, 55, 55); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px;">要理解下面的代码的话, 要知道的一个知识点是 协议(protocal)

文件:ClassA.h
#import <Foundation/Foundation.h>
#import "ClassB.h"
@interface ClassA : NSObject<SampleProtocal>
- (void)test;
@end


文件:ClassA.m
#import "ClassA.h"
@implementation ClassA


-(void)test{
    ClassB *classB = [[ClassB alloc] init];
    classB.delegate = self;
    
    [classB actionOfClassB];
}


-(void)callback{
    NSLog(@"这个方法会被类B调用");
}
@end


文件:ClassB.h
#import <Foundation/Foundation.h>

@protocol SampleProtocal <NSObject>
@required
- (void)callback;
@end


@interface ClassB : NSObject{
    id<SampleProtocal> delegate;
}
@property (nonatomic, retain) id<SampleProtocal> delegate;


- (void)actionOfClassB;


@end


文件:ClassB.m
#import "ClassB.h"


@implementation ClassB
@synthesize delegate;
-(void)actionOfClassB{
    [delegate callback];
}
@end
</span>
<span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#373737;"><span style="font-size: 15px;">(转载:http://blog.csdn.net/mars2639/article/details/7310182)</span></span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值