iOS开发之protocol和delegate

来源:互联网 发布:淘宝刷单平台 php源码 编辑:程序博客网 时间:2024/05/19 12:14
 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; background-color: rgb(231, 229, 220); width: 912.765625px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">view plain</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">copy</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">print</a><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">?</a><div style="position: absolute; left: 401px; top: 829px; width: 22px; height: 11px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="11" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=22&height=11" wmode="transparent" /></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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; background-color: inherit; font-weight: bold;">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-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); color: rgb(85, 85, 85); line-height: 18px;"><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; background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)methodB;  </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); color: rgb(85, 85, 85); line-height: 18px;"><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; background-color: rgb(231, 229, 220); width: 912.765625px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">view plain</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">copy</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">print</a><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">?</a><div style="position: absolute; left: 401px; top: 1102px; width: 22px; height: 11px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="11" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=22&height=11" wmode="transparent" /></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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; background-color: rgb(231, 229, 220); width: 912.765625px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">view plain</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">copy</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">print</a><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">?</a><div style="position: absolute; left: 401px; top: 1345px; width: 22px; height: 11px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="11" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=22&height=11" wmode="transparent" /></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); color: rgb(85, 85, 85); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">[b methodA];  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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; background-color: rgb(231, 229, 220); width: 912.765625px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">view plain</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">copy</a><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; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">print</a><a target=_blank href="http://blog.csdn.net/mars2639/article/details/7310182#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px;">?</a><div style="position: absolute; left: 401px; top: 1563px; width: 22px; height: 11px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="22" height="11" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=22&height=11" wmode="transparent" /></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; 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-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); color: rgb(85, 85, 85); line-height: 18px;"><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; background-color: rgb(255, 255, 255); display: inline !important;"><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>
0 0