文章出處
View Code
View Code
View Code
文章列表
1.通過中介公司找房子

//1:協議聲明: #import <Foundation/Foundation.h> @protocol FindApartment <NSObject> -(void)findApartment; @end //2:代理角色聲明(Agent.h)頭文件聲明 #import <Foundation/Foundation.h> #import "FindApartment.h" @interface Agent : NSObject <FindApartment> @end //3:代理角色實現(Agent.m)實現文件 #import "Agent.h" @implementation Agent -(void)findApartment{ NSLog(@"findApartment"); } @end //4:真實角色聲明(Person.h)頭文件聲明 #import <Foundation/Foundation.h> #import "FindApartment.h" @interface Person : NSObject { @private NSString *_name; id<FindApartment> _delegate; //委托人(具備中介找房子的協議) } @property(nonatomic,copy) NSString *name; @property(nonatomic,assign)id<FindApartment> delegate; -(id)initWithName:(NSString *)name withDelegate:(id<FindApartment>) delegate; -(void)wantToFindApartment; @end //5:真實角色實現(Person.m)實現 #import "Person.h" //定義私有方法 @interface Person() -(void)startFindApartment:(NSTimer *)timer; @end @implementation Person @synthesize name=_name; @synthesize delegate=_delegate; -(id)initWithName:(NSString *)name withDelegate:(id<FindApartment>) delegate{ self=[super init]; if(self){ self.name=name; self.delegate=delegate; } return self; } -(void)wantToFindApartment{ [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@"Hello" repeats:YES]; } -(void)startFindApartment:(NSTimer *)timer{ //NSString *userInfo=[timer userInfo]; [self.delegate findApartment]; } @end //6:測試代理main.m方法 #import <Foundation/Foundation.h> #import "Person.h" #import "Agent.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); Agent *agent=[[Agent alloc]init]; Person *jack=[[Person alloc]initWithName:@"jack" withDelegate:agent]; [jack wantToFindApartment]; [[NSRunLoop currentRunLoop]run]; [jack autorelease]; } return 0; }
2.代理設計模式的編碼規范:
(1)一幫情況下,當協議屬于誰,就將協議定義到誰的頭文件中
(2)協議的名稱一般以它屬于那個類的類名開頭,后面跟上protocol或delegate
(3)協議中的方法名稱一般以協議的名稱protocol之前的作為開頭
(4)一般情況下,協議中的方法會將觸發該協議的對象傳遞出去
(5)一般情況下,一個類中的代理的名稱叫做 delegate;
(6)當一個類要成為另外一個類的代理的時候,一般情況下:
.h中用 @protocol 協議名稱; 來告訴當前類,這是一個協議。
.m中 用 #import 類名 來真正導入協議的聲明
例子:學生找房子

Student.h #import <Foundation/Foundation.h> @class Student; //通過代理來找房子,所以首先定義一個協議 @protocol StudentProtocol <NSObject> -(void)studentFindHourse:(Student *)student; @end @interface Student : NSObject //代理 @property(nonatomic, strong) id<StudentProtocol> delegate; //找房子 -(void)findHourse; @end ---------------------------------------------------------------------------------------- Student.m #import "Student.h" @implementation Student //學生通過代理找房子 -(void)findHourse{ NSLog(@"學生想找房子"); if ([self.delegate respondsToSelector:@selector(studentFindHourse:)]) { [self.delegate studentFindHourse:self]; } } @end ---------------------------------------------------------------------------------------- LoveHome.h #import <Foundation/Foundation.h> @protocol StudentProtocol;//標明StudentProtocol是一個協議 //愛家,專做銷售房子的代理 @interface LoveHome : NSObject<StudentProtocol> @end ---------------------------------------------------------------------------------------- LoveHome.m #import "LoveHome.h" #import "Student.h" @implementation LoveHome -(void)studentFindHourse:(Student *)student{ NSLog(@"%s 幫忙找房子",__func__); } @end ---------------------------------------------------------------------------------------- main.m #import <Foundation/Foundation.h> #import "Student.h" #import "LoveHome.h" int main(int argc, const char * argv[]) { Student *stu = [[Student alloc] init]; LoveHome *lh = [[LoveHome alloc] init]; stu.delegate = lh; [stu findHourse]; return 0; }
根據規范,重寫1:

//Person.h #import <Foundation/Foundation.h> @class Person; @protocol PersonProtocol <NSObject> -(void)personWantToFindApartment:(Person *)person; @end //Person要找房子 @interface Person : NSObject @property (nonatomic, assign) NSString *name; //找房子的代理 @property (nonatomic, strong) id<PersonProtocol>delegate; //構造方法 -(instancetype)initWithName:(NSString *)name withDelegate:(id<PersonProtocol>)delegate; //找房子 -(void)wantToFindApartment; @end //Person.m #import "Person.h" //定義私有方法 @interface Person() -(void)startFindApartment:(NSTimer *)timer; @end //實現Person @implementation Person -(instancetype) initWithName:(NSString *)name withDelegate:(id<PersonProtocol>)delegate{ self = [super init]; if (self) { self.name = name; self.delegate = delegate; } return self; } -(void) wantToFindApartment{ [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@"代理找房子" repeats:YES]; } //實現私有方法 -(void) startFindApartment:(NSTimer *)timer{ NSString *info = [timer userInfo]; NSLog(@"1"); if ([self.delegate respondsToSelector:@selector(personWantToFindApartment:)]) { NSLog(@"%@",info); [self.delegate personWantToFindApartment:self]; } } @end //Agent.h //代理 #import <Foundation/Foundation.h> @protocol PersonProtocol; @interface Agent : NSObject<PersonProtocol> @end //Agent.m #import "Agent.h" #import "Person.h" @implementation Agent -(void)personWantToFindApartment:(Person *)person{ NSLog(@"%s 代理幫忙找房子",__func__); } @end //main.m #import <Foundation/Foundation.h> #import "Person.h" #import "Agent.h" int main(int argc, const char * argv[]) { @autoreleasepool { Agent *a = [[Agent alloc] init]; Person *p = [[Person alloc] initWithName:@"keen" withDelegate:a]; [p wantToFindApartment]; [[NSRunLoop currentRunLoop] run]; } return 0; }
文章列表
全站熱搜