IOS --- common block usage

Source: Internet
Author: User

IOS --- common block usage

The block in Objective-C has multiple definitions and usage methods.

As property
@property (nonatomic, copy) int (^myBlock)(int a, int b);

Block Code body:

_myBlock = ^int (int a, int b) {    return a + b;};

Usage:

sum = _myBlock(10, 20);
Use typedef
typedef int (^MyBlock)(int a, int b);MyBlock myBlock = ^int (int a, int b) {    return a * b;};

Usage:

int sum = myBlock(10, 20);
As a variable
int (^myBlock) (int a, int b) = ^int (int a, int b) {    return a - b;};int sum = myBlock(10, 20);

In this case, myBlock can be freely transmitted as a variable.MyBlock (10, 20 );You can.
To modify the current local variable in the block, use _ block:

__block int sum = 0;int (^myBlock)(int a, int b) = ^int (int a, int b) {    sum = a + b;    return sum;}

Block can access the local variable sum by default, but cannot be modified to prevent circular references.
The _ block object will not be strongly referenced once in the block, so circular references will not appear.

_ Block and _ weak

As we can see above, when declaring a block, we only copy the sum local variable. Therefore, if it is a pointer, we do not need to add _ block to modify the content it points to in the block.
_ Block modifies objects and basic data types, while _ weak can only modify objects.
_ The block object can be modified in the block (re-assigned), but _ weak cannot.
Therefore, to modify the attributes of a class object in a block, use _ weak. For example:

__weak MyClass *weakSelf = self;_myBlock2 = ^(NSInteger count) {     weakSelf.count = count;}
Used as a method call Parameter

Pre-declare MyBlock and Its Attributes myBlock2,

typedef int (^MyBlock)(int a, int b);@property (nonatomic, copy) MyBlock myBlock2;

Define method methodTakeBlock to receive MyBlock.

- (int)methodTakeBlock:(MyBlock)block {    int sum = 0;    if (block) {        sum = block(10, 20);    }    return sum;}

When this method is called, The MyBlock object is implemented in its parameters:

sum = [self methodTakeBlock:^int (int a, int b) {    return b / a;}];

This method is only available in implementation.

Specify the block type in the method declaration.

In the interface:

// Method call parameter-(int) method2TakeBlock :( int (^) (int a, int B) block;

In implementation:

- (int)method2TakeBlock:(int (^)(int, int))block {    int sum = 0;    if (block) {        sum = block(10, 20);    }    return sum;}

Call method:

sum = [self method2TakeBlock:^int(int a, int b) {    return a * b - b;}];
Transmit data between ViewController

Define a block in TestViewController. h to modify the label content in ViewController when redirecting from TestViewController to ViewController:

#import 
  
   @interface TestViewController : UIViewControllertypedef void(^BlockUpdateBtnTitle)(NSString *);@property (nonatomic, copy) BlockUpdateBtnTitle blockUpdateBtnTitle;@end
  

This block receives an NSString parameter.
Click button to trigger the following actions

- (IBAction)action:(UIButton *)sender {    if (_blockUpdateBtnTitle) {        _blockUpdateBtnTitle(@value changed by block);    }    [self dismissViewControllerAnimated:NO completion:nil];}

Pass the block object in ViewController. m. Note that the received parameters must be consistent with the defined ones:

- (IBAction)action:(UIButton *)sender {    TestViewController *testVC = [[TestViewController alloc] init];    __weak ViewController *weakSelf = self;    testVC.blockUpdateBtnTitle = ^(NSString *btnTitle) {        weakSelf.lb.text = btnTitle;    };    [self presentViewController:testVC animated:NO completion:nil];}

Click the button to jump to TestViewController. Execute this blockUpdateBtnTitle in TestViewController to modify the label content in ViewController.
Because the attribute in ViewController needs to be modified in the block, you can use _ weak to prevent circular reference.

 

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.