The block code block reversely transmits values, and the block code transmits values.

Source: Internet
Author: User

The block code block reversely transmits values, and the block code transmits values.

Block Code blocks are also called closures. Similar to C functions, block code blocks provide reverse value transfer and callback functions.

There are two block formulas:

① Description and implementation of the formula written into one piece

Return Value Type (^ block name) (parameter list parameter type parameter name) = ^ (parameter list parameter type parameter name ){

Implementation Code (if a return value requires a return value type value)

};

Call: block name (parameter );

 

② Description and implementation separate writing Formula

(1) Statement

Return Value Type (^ block name) (parameter list );

(2) Implementation

Block name = ^ (parameter list ){

Implementation Code (if a return value requires a return value type value)

};

(3) Call

Block name (real parameter );

Note: The producer must have an implementation method before calling it.

 

Here we will focus on the block's value passing in the direction (the value passing in the direction is to give it a value during the callback). Let's use the button example to explain how the block transfers the value in the reverse direction.

The Code is as follows:

Create a window with navigation bar in AppDelegate. m

#import "AppDelegate.h"#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];     self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];     [self.window makeKeyAndVisible];     return YES; } @end

Create two buttons in the ViewController. m file, and create a class that inherits from the name of UIViewController BlockViewController. Import the buttons to the ViewController. m file.

# Import "ViewController. h "# import" BlockViewController. h "@ interface ViewController () {UIButton * testButton;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; [self createButton];}-(void) createButton {// put the image's button testButton = [UIButton buttonWithType: UIButtonTypeCustom]; testButton. frame = CGRectMake (150,100,100,100); testButton. backgroundColor = [UIColor brownColor]; testButton. layer. cornerRadius = 50; testButton. layer. masksToBounds = YES; [self. view addSubview: testButton]; // click the UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; button. frame = CGRectMake (100,250,200, 50); [button setTitle: @ "go to the next page" forState: UIControlStateNormal]; button. backgroundColor = [UIColor brownColor]; button. layer. cornerRadius = 6; [button addTarget: self action: @ selector (clickButton) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button];} // trigger event of the button-(void) clickButton {BlockViewController * block = [[BlockViewController alloc] init]; // block implementation block. buttonBlock = ^ (UIImage * image) {[testButton setImage: image forState: UIControlStateNormal] ;}; [self presentViewController: block animated: YES completion: nil] ;}@ end

Declare the block Method in your own BlockViewController. h file and use the block as an attribute.

# Import <UIKit/UIKit. h> @ interface BlockViewController: UIViewController // block declaration here the block is treated as the attribute @ property (nonatomic, copy) void (^ buttonBlock) (UIImage * image); @ end

Implement block callback in the BlockViewController. m file

# Import "BlockViewController. h "@ interface BlockViewController () {UIButton * testButton;} @ end @ implementation BlockViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; [self backButton];}-(void) backButton {testButton = [UIButton buttonWithType: UIButtonTypeCustom]; testButton. frame = CGRectMake (150,100,100,100); [testButton setImage: [UIImage imageNamed: @ "header 3.jpg"] forState: UIControlStateNormal]; testButton. backgroundColor = [UIColor brownColor]; testButton. layer. cornerRadius = 50; [self. view addSubview: testButton]; UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; button. frame = CGRectMake (100,250,200, 50); [button setTitle: @ "back to previous page" forState: UIControlStateNormal]; button. backgroundColor = [UIColor brownColor]; button. layer. cornerRadius = 6; [button addTarget: self action: @ selector (clickButton) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button];}-(void) clickButton {// block callback self. buttonBlock ([UIImage imageNamed: @ "header 3.jpg"]); // return to the previous page [self dismissViewControllerAnimated: YES completion: nil];} @ end

 

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.