[Basic usage of block in Hmly]10.ios

Source: Internet
Author: User
Tags uikit

Introduction to this article

This chapter does not do much research on block implementations. Just explain the basic usage. Purely basic knowledge. The actual project how to do the example. Block using the scene, can be in the two interface of the value, you can also code encapsulation as a parameter of the pass, and so on. Use GCD to know the subtleties of the block.

About Block

Block is a very special type of data. It can save a piece of code and fetch it at the appropriate time to invoke it.

The modification of block

In the case of arc
1. If the block is modified with copy, the block is stored in the heap space. The internal object of the block is strongly referenced, resulting in a circular reference. Memory cannot be freed.
Workaround:
Create a new pointer (__weak typeof (target) Weaktarget = target) and point to the object in the block code, and then use Weaktarget to do so. You can solve circular reference problems.

2. If the block is modified with weak, the block will be stored in the stack space. There is no circular reference issue.

MRC Case
With copy retouching, if you want to use objects inside a block, you need to process (__block typeof (target) Blocktarget = target). Operate with Blocktarget in block.

Block's definition Format

return value type (^block variable name) (parameter list) = ^ (parameter list) {
};
Calling block-saved code
Block variable name (argument);

By default, the outer local variables cannot be modified inside the block
Local variables modified using __block can be changed inside the block

Block's mode

1. Block without parameters and no return value
2. Block with parameter no return value
3. Block with parameters and return values

Block with no parameter and no return value

/** *  无参数无返回值的Block */-(void)func1{    /**     *  void :就是无返回值     *  emptyBlock:就是该block的名字     *  ():这里相当于放参数。由于这里是无参数,所以就什么都不写     */    void (^emptyBlock)() = ^(){        NSLog(@"无参数,无返回值的Block");    }; emptyBlock();}


Block with parameter no return value

/**     *  调用这个block进行两个参数相加     *     *  @param int 参数A     *  @param int 参数B     *     *  @return 无返回值     */    void (^sumBlock)(int ,int ) = ^(int a,int b){ NSLog(@"%d + %d = %d",a,b,a+b); }; /** * 调用这个sumBlock的Block,得到的结果是20 */ sumBlock(10,10);

Block with parameters with return value

/**     *  有参数有返回值     *     *  @param NSString 字符串1     *  @param NSString 字符串2     *     *  @return 返回拼接好的字符串3     */        NSString* (^logBlock)(NSString *,NSString *) = ^(NSString * str1,NSString *str2){ return [NSString stringWithFormat:@"%@%@",str1,str2]; }; //调用logBlock,输出的是 我是Block NSLog(@"%@", logBlock(@"我是",@"Block"));

Block combined with typedef

It is simpler and more convenient to define a block type by itself and create blocks with defined types.
Here, for example, a block callback modifies the background color of the previous interface.
ViewController1 Controller 1,viewcontroller2 Controller 2
Controller 1 jumps to controller 2, and then the Controller 2 triggers an event callback to modify the background color of controller 1 to red.

The realization of ViewController2

 #import  <uikit/uikit.h> @interface  ViewController2: uiviewcontroller/** * defines a changecolor block. This changecolor must take a parameter, the type of the parameter must be an ID type of * no return value * @param ID */typedef void (^changecolor) (id);  /** * Using the ChangeColor defined above to declare a block, the block declared must comply with the requirements of the Declaration. */ @property (nonatomic, copy) ChangeColor BackgroundColor;  @end                 


-(void) TouchesBegan: (nsset<uitouch *> *) touches withevent: (uievent *) event{//declares a color UIColor *color = [ Span class= "hljs-built_in" >uicolor Redcolor]; //with the block just declared to modify the background color of the previous interface self.backgroundcolor (color);}  
< Span class= "Hljs-keyword" >< Span class= "hljs-built_in" > Implementation of ViewController1 

-(void) TouchesBegan: (nsset<uitouch *> *) touches withevent: (uievent *) event{ViewController2 *VC =[[viewcontroller2 Alloc]init]; //callback Modify Color Vc.backgroundcolor = ^ (uicolor *color) {self.view.backgroundcolor = color; }; [self.navigationcontroller pushviewcontroller:vc animated: YES];}           
 

[Basic usage of block in Hmly]10.ios

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.