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