【iOS開發】協議與委托 (Protocol and Delegate) 實例解析 --- 轉(http://www.dapps.net/dev/iphone/ios-dev-protocol-and-delegate-example.html)
@import url(http://www.shnenglu.com/cutesoft_client/cuteeditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);1 協議:
協議,類似于Java或C#語言中的接口,它限制了實現類必須擁有哪些方法。
它是對對象行為的定義,也是對功能的規范。
示例:
1 2 3 4 5 6 7 8 9 |
// GoodChild.h
#import <Foundation/Foundation.h> @protocol GoodChild <NSObject> -(void)filialPiety; @end |
1 2 3 4 5 6 7 8 |
// Student.h
#import <Foundation/Foundation.h> #import "GoodChild.h" //注意實現協議的語法。 @interface Student : NSObject<GoodChild> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Student.m
// protocol // // Created by sxt on 11-10-23. // Copyright 2011年 __MyCompanyName__. All rights reserved. // #import "Student.h" @implementation Student - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } -(void)filialPiety{ NSLog(@"孝敬父母。。"); } @end |
此例中定義了一個協議GoodChild,類Student實現了此協議,所以必須有filialPiety方法。
每個類雖只有一個父類,但可以實現多個協議,以逗號隔開便可。語法如下:
1 2 3 |
@interface Student : NSObject<協議1,協議2>
@end |
2 委托:
委托是objC中使用非常頻繁的一種設計模式,它的實現與協議的使用是分不開的,讓我們看一個綜合示例:
小公司老板日常的工作是管理公司、教導新員工、發工資與接電話。
其中管理公司、教導新員工是老板要親為的。
而發工資與接電話老板希望招聘一個秘書來幫忙,于是對秘書的要求就是要略懂出納發工資,要能幫助領導接電話。 而這兩項要求便是協議,對類功能的限定。
1 2 3 4 5 6 7 8 9 10 11 |
// SecProtocol.h
#import <Foundation/Foundation.h> @protocol SecProtocol <NSObject> //發工資 -(void)payoff; //接電話 -(void)tel; @end |
然后定義一個秘書類
1 2 3 4 5 6 7 |
// Sec.h
#import <Foundation/Foundation.h> #import "SecProtocol.h" @interface Sec : NSObject<SecProtocol> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Sec.m
#import "Sec.h" @implementation Sec - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } -(void)payoff{ NSLog(@"sec payoff"); } -(void)tel{ NSLog(@"sec tel"); } @end |
緊接著是老板類:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Boss.h
#import <Foundation/Foundation.h> #import "SecProtocol.h" @interface Boss : NSObject //此屬性用于指定秘書對象,此對象必須實現SecProtocol協議。 @property(nonatomic,retain) id<SecProtocol> detegate; //管理 -(void)manage; //教導新員工 -(void)teach; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// Boss.m
#import "Boss.h" @implementation Boss @synthesize detegate=_detegate; - (id)init { self = [super init]; if (self) { // Initialization code here. } return self; } -(void)manage{ NSLog(@"boss manage"); } -(void)teach{ NSLog(@"boss teach"); } -(void)payoff{ NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init]; [_detegate payoff]; [p release]; } -(void)tel{ NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init]; [_detegate tel]; [p release]; } @end |
那么老板就具有這4個方法,當調用前2個時是自己完成功能,而調用后2個時則轉為調用秘書的方法。
此時我們跟秘書對象就叫做代理對象,代理模式的名字由來于此。
最后調用測試下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// main.m
// delegate // // Created by sxt on 11-10-23. // Copyright 2011年 Jinlong Wei. All rights reserved. // #import <Foundation/Foundation.h> #import "Boss.h" #import "Sec.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //實例化老板對象 Boss *boss=[[[Boss alloc] init] autorelease]; //實例化秘書對象 Sec *sec=[[[Sec alloc] init] autorelease]; //設置老板的代理對象為秘書 boss.detegate=sec; //調用4個方法。 [boss payoff]; [boss tel]; [boss manage]; [boss teach]; [pool drain]; return 0; } |