main.m文件 Agent.h Agent.m Person.h Person.m ...
main.m文件
#import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... Person *xw = [Person new]; [xw needHouse:@"小區房"]; } return 0; }
Agent.h
#import <Foundation/Foundation.h> @protocol rentHouseDelegate<NSObject> - (void) rentHouseDidFinished:(NSString *)result; @end @interface Agent : NSObject @property (nonatomic,assign) id <rentHouseDelegate>delegate; - (NSString *)rentHouse; @end
Agent.m
#import "Agent.h" @implementation Agent - (NSString *)rentHouse{ if (self.delegate != nil) { NSString *result = @"房子找到了,為**小區3棟405"; [self.delegate rentHouseDidFinished:result]; return result; } return nil; } @end
Person.h
#import <Foundation/Foundation.h> #import "Agent.h" @interface Person : NSObject<rentHouseDelegate> - (void) needHouse:(NSString *)require; @end
Person.m
#import "Person.h" @implementation Person - (void)needHouse:(NSString *)require{ //1.找到中介(需要導入中介類) Agent *agent = [Agent new]; //2.告訴中介我是誰,(在這裡就需要在中介類定義一個屬性進行記錄) agent.delegate = self; //3.中介去租房子,(在這裡需要在中介類裡面定義一個租房子的方法) [agent rentHouse]; } - (void)rentHouseDidFinished:(NSString *)result{ NSLog(@"%@",result); } @end