Second, Block Analysis of iOS, iosblock callback

Source: Internet
Author: User

Second, Block Analysis of iOS, iosblock callback

========================================================== ====================================

| Reprinted must indicate the blog address: http://www.cnblogs.com/devappnow |

| Respect originality and others' labor achievements. If you feel helpful to the reader, you can also encourage the blogger in any way (such as recommendations, comments, and emails ). |

========================================================== ====================================

The Block mechanism in iOS simplifies the program and implements the callback function. Similar to function pointers in C. You can call back functions by passing blocks.

Simple Example:

Define a block

Int (^ myblock) (int a, int B );

Explanation: a block is defined and its name is myblock (similar to a function pointer). Its return type is int, and two int-type a and B parameters are passed in.

  

Define the specific content of the function pointer. (Function body)

Myblock = ^ (int a, int B ){

Retrun a + B;

};

Explanation: defines the specific functions of myblock. Return results based on the input two int parameters.

 

Let's look at a more complex object parameter block.

This block is used to connect two strings.

// Simple instance 3

NSString * (^ concatString) (NSString * s1, NSString * s2) = ^ (NSString * s1, NSString * s2 ){

Return [s1 stringByAppendingString: s2];

};

NSLog (@ "% @", concatString (@ "hello", @ "world"); // print: hello world

More block-based methods are used in the program, namely block callback. It is to pass the block address to another interface. After processing the relevant data on the other interface, some data needs to be returned

First interface, and then let the previous interface process. Such business logic is very common.

Example:

When registering A user, you must select the address from the address list on the current page A (another page B). After selecting your address, you need to upload the address back to interface A for processing.

You can use block to complete this operation. Of course. You can also use the proxy protocal. Block is discussed here.

The statement is as follows:

Define a block in B.

Here we use the typedef key to establish. Typedef void (^ myBlock) (int a, int B );

The. h file on interface B is as follows:

1 #import <UIKit/UIKit.h>2 3 typedef void(^myBlock)(int a, int b);4 5 @interface SecondViewController : UIViewController6 7 -(void)getTwoNumber:(myBlock) block;8 9 @end

The. m file on interface B is as follows:

1 # import "SecondViewController. h "2 3 @ implementation SecondViewController 4 5-(void) viewDidLoad {6 [super viewDidLoad]; 7 // Do any additional setup after loading the view from its nib. 8 self. view. layer. backgroundColor = [UIColor blueColor]. CGColor; 9} 10 11-(void) getTwoNumber :( myBlock) block {12 // suppose there is a complicated operation here, and finally two values are obtained, x and y. 13 int x = 12; 14 int y = 13; 15 16 // return x and y to call the block method defined in interface A for processing. 17 block (x, y); 18 19} 20 21-(void) didReceiveMemoryWarning {22 [super didreceivemorywarning]; 23 // Dispose of any resources that can be recreated.24} 25 26 @ end

Interface B has been fully written. Now you need to call the method of interface B on interface. The statement is as follows:

SecondViewController * svc = [[SecondViewController alloc] init];

[Svc getTwoNumber: ^ (int a, int B ){

NSLog (@ "value a, B: % d, % d", a, B );

}];

[Self. navigationController pushViewController: svc animated: YES];

 

In this way, the callback is implemented. In fact, the above callback has been made when the getTwoNumber method of svc is called on interface.

In fact, B should call back the block for a certain period of time in the future. How to do it.

Declare B _myblock in B and save the block address passed by. The Code is as follows:

 1 #import <UIKit/UIKit.h> 2  3 typedef void(^myBlock)(int a, int b); 4  5 @interface SecondViewController : UIViewController 6  7 @property (strong, nonatomic) myBlock b_myblock; 8  9 -(void)getTwoNumber:(myBlock) block;10 11 @end

In interface B, a button is defined and a click event is added. When you click it, A parameter is passed in, which calls back the block in A and processes the parameter in.

1 # import "SecondViewController. h "2 3 @ implementation SecondViewController 4 5-(void) viewDidLoad {6 [super viewDidLoad]; 7 // Do any additional setup after loading the view from its nib. 8 self. view. layer. backgroundColor = [UIColor blueColor]. CGColor; 9} 10 11-(void) getTwoNumber :( myBlock) block {12 _ B _myblock = block; 13} 14-(IBAction) buttonClick :( id) sender {15 int x = 12; 16 int y = 13; 17 18/ /Return x and y to call the block method defined in interface A for processing. 19 _ B _myblock (x, y); 20} 21 22-(void) didReceiveMemoryWarning {23 [super didreceivemorywarning]; 24 // Dispose of any resources that can be recreated.25} 26 27 @ end

================================ Lili's line ============

Summary.

 

 

 

 

 

 

 

========================================================== ====================================

| Reprinted must indicate the blog address: http://www.cnblogs.com/devappnow |

| Respect originality and others' labor achievements. If you feel helpful to the reader, you can also encourage the blogger in any way (such as recommendations, comments, and emails ). |

========================================================== ====================================

  

Related Article

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.