Main.m
//
Main.m
Callback
//
Created by hehe on 15/9/10.
Copyright (c) 2015 Wang.hehe. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Employee.h"
#import "Company.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
Create a Company object
Company *company = [[Company Alloc] init];
Create an Employee object
Employee *emp = [[Employee Alloc]init];
Set up agent relationships
company.emp = EMP;
Mobilization
[Company DoWork];
}
return 0;
}
//employee.h
/* When the item is sold, it needs to call block, and its corresponding code
Set the implementation callback in the client's Party */
#import <Foundation/Foundation.h>
@interface Employee:nsobject
has a block-type property that implements callbacks through it
Block set to property requires that his memory be copy
@property (nonatomic,copy) void (^block) ();
-(void) sellproduct;
@end
//EMPLOYEE.M
//
Employee.m
Callback
//
Created by hehe on 15/9/10.
Copyright (c) 2015 Wang.hehe. All rights reserved.
//
#import "Employee.h"
@implementation Employee
-(void) sellproduct
{
NSLog (@ "Selling goods");
When an employee sells a product, it needs feedback to tell the company
Callback
Self.block ();
}
@end
Company.h
/*
Company
Principal party
*/
#import <Foundation/Foundation.h>
#import "Employee.h"
@interface Company:nsobject
Set the properties of the agent
@property (Nonatomic,strong) Employee *emp;
How to mobilize employees to sell
-(void) doWork;
@end
//company.m
//
Company.m
Callback
//
Created by hehe on 15/9/10.
Copyright (c) 2015 Wang.hehe. All rights reserved.
//
#import "Company.h"
@implementation Company
-(void) doWork
{
Code to set callback
//.. is a statement. //
Self.emp.block = ^ () {
NSLog (@ "I have completed the sales task");
};
Principal agent (employee to sell)
[Self.emp sellproduct];
}
@end
Block Callback Concrete Example