Block Callback Concrete Example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.