Getting started with Block and mblock

Source: Internet
Author: User

Getting started with Block and mblock

At the beginning of iOS4.0, Block was born. It is actually a supplement to c's predictions. The book points out that it is an anonymous function with automatic variables. The Block is concise and the code is highly readable, therefore, it is favored by developers. This time, we will introduce the basic types of Block and the actual operations in the project.

The Block format is as follows:

Basic Type of Block 1. No parameter no return value
Void (^ tempBlock) () = ^ () {NSLog (@ "No parameter no return value") ;}; // call tempBlock ();

 

2. No parameters have returned values
Int (^ tempBlock) () = ^ () {return 10 ;}; // when calling, 10 is returned no matter what you enter; tempBlock (100 );

 

3. There are parameters that do not return values
Void (^ tempBlock) (int) = ^ (int temp) {NSLog (@ "no return value for parameters ");};

 

4. Parameters with returned values
Int (^ tempBlock) (int) = ^ (int number) {return number ;}; // print the number of input tempBlock (100 );

 

Typical use cases of Block 1. Modify external variables
_ Block int x = 100; void (^ sumXWithYBlock) (int) = ^ (int y) {x = x + y; NSLog (@ "new value % d ", x) ;}; // The printed value is x + y, 100 + 100 = 200 sumXWithYBlock (100 );

 

2. Pass values between pages
// 1. on the second page (SecondViewController), first declare an attribute/** first declare the block name and determine the parameter type */@ property (nonatomic, copy) void (^ netViewBlock) (NSString * text); // 2. when you click the button to return the result, you need to pass back the parameters you need. The parameter types must be consistent-(void) back {self. netViewBlock (@ "hello"); [self. navigationController popViewControllerAnimated: YES];}

 

3. On the first page (FirstViewController), get the attributes of viewcontroller2.
-(Void) click :( UIButton *) sender {// display the returned value on the second page on the label UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (30,100,200, 30)]; [self. view addSubview: label]; SecViewController * vc = [[SecViewController alloc] init]; vc. netViewBlock = ^ (NSString * text) {label. text = text;}; [self. navigationController pushViewController: vc animated: YES];}

 

Custom Block

Example: When you click a Button, you need to change the title of the Button.

Implementation:

1. Create a tool class, declare a class method, and customize a block. The title is required, so the parameter type is NSString.

@interface ChangeBuTitleTool : NSObject+(void)changeBuTitleWithText:(void(^)(NSString *titleText))text;@end

2. Implementation

@implementation ChangeBuTitleTool+(void)changeBuTitleWithText:(void(^)(NSString *titleText))text{  if (text) {      text(@"tyler");  }}@end

3. click the Button in the Controller to change the title.

-(void)addButton{  UIButton *bu = [UIButton buttonWithType:(UIButtonTypeCustom)];  bu.backgroundColor = [UIColor blueColor];  bu.frame = CGRectMake(30, 90, 100, 50);  [self.view addSubview:bu];  [bu addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];    }-(void)click:(UIButton *)sender{  [ChangeBuTitleTool changeBuTitleWithText:^(NSString *titleText) {      [sender setTitle:titleText forState:(UIControlStateNormal)];  }]; }

4. Combination of Block and typedef

In the previous example, declare a class method, and define the block to be written directly in the class method. It looks very unfriendly, especially for beginners who do not look very readable. You can use typedef to define a block separately, increase the readability of the Code.

typedef void (^titleBlock)(NSString *titleText);@interface ChangeBuTitleTool : NSObject+(void)changeBuTitleWithText:(titleBlock)text;//+(void)changeBuTitleWithText:(void(^)(NSString *titleText))text;@end@implementation ChangeBuTitleTool+(void)changeBuTitleWithText:(titleBlock)text{  if (text) {      text(@"tyler");  }}@end

 

Here is the introduction of the Block entry, which will be more exciting in the next period! <( ̄)>

 

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.