Some ways to use IOS---block (back-propagation)

Source: Internet
Author: User
Tags uikit

A friend recently asked me how the block in iOS was used,

I told him about some of the practical applications I had learned.

Because I think this is mostly more practical,

Well nonsense not much to say that directly on the actual



------

In fact, using the block pass is usually the value, a page--> b page If it is positive, then we pass the value of the B page to A is called the reverse transfer value (or callback).

Steps:


1. Who sent the message, who wrote block (with the use of agents)

#import <UIKit/UIKit.h>

typedef void (^myblock) (NSString *str);


@interface Detailviewcontroller:uiviewcontroller

//Generate a block property
@property (nonatomic, copy) Myblock block;< c4/> @end

2. In the b.m file to pass the value---when the b page to disappear, call their own block pass value

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
    //calls its own block to pass it an argument
    nsstring *str = @ "Hello I am detail page incoming string";

    Block is based on the C language function, direct parameter invocation < specific implementation in the message receiver a>
    self.block (str);
    
    [Self Dismissviewcontrolleranimated:yes completion:^{
        
    }];
}


3. It is in the a page, received the value of B page passed-----< in the creation of a B page class instance, the implementation b.block receive its incoming parameters >

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
    Detailviewcontroller *detail = [ Detailviewcontroller alloc] init];
   
    Direct call to detail block implementation
    Detail.block = ^ (NSString *str) {
    
        //print incoming str
        NSLog (@ "str:%@", str);
    
    //Jump to Detail page
    [Self presentviewcontroller:detail animated:yes completion:^{
        
    }];
}

----------------------------This is the 3 songs used in block syntax--------------------

How is it not very simple.

Here's a look at the effect chart







============ Rear Extension ==========

Block of some of the small definition, the estimated understanding of the above has been a little read, only for reference

1.block can be used to save a piece of code, or to encapsulate a piece of code. ---> Code snippet, code block.

The logo of the 2.block is ^

3.block is like a function (in fact it is the C language function)

can save the code;

Can have a return value;

Can also have the row parameter;

Call the same way;

4. Define a block

#pragma mark block with no parameters

void (^outputblock) = ^{

NSLog (@ "------------");

};

Outputblock ();

}


5. Block with row parameters

#pragma mark-The block with parameters

Int (^sumblock) (int, int) = ^ (int a,int b) {

return a+b;

};

int sum = Sumblock (3,13);

int sum1 = Sumblock (1,2);

NSLog (@ "%d", sum1);

NSLog (@ "%d", sum);


#pragma mark-Output n horizontal line

The Declaration and implementation of block

void (^myblock) (int) = ^ (int n) {

for (int i = 0; i < n; i++) {

NSLog (@ "-----------------");

}
};

Call to block

Myblock (10);


6. If the block has no row parameters, you can omit the parentheses behind it.

7. Use a typedef to define the type of a block

typedef int (^myblock) (int,int);

Myblock Sumblock = ^ (int a,int b) {

return a+b;

}

Myblock minus = ^ (int a,int b) {

return a-b;

}

8.block can access variables outside, or modify external local variables

Must declare in advance

For example __block int a = 5;

A variable modified with __block can modify its value when the block is called


Postscript:

There's a lot of little knowledge about block, though.

Here I will first say so many, mainly can be practical, later I will as soon as possible to complete the knowledge of the block to write up for all the friends need to refer to.





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.