Basic use of IOS block and block pass value

Source: Internet
Author: User

Block provides us with a very convenient way to implement various values and callbacks

Reasonable use of block reduces the amount of code and more elegant implementation capabilities

Now do a small collation as follows:

#pragma mark about block//block's prototype: NSString * (^myblock) (int);
    
    The code above declares a block (^) prototype, named Myblock, that contains an int parameter, and returns a pointer to the NSString type.
    Block definition: myblock=^ (int parama) {return [NSString stringwithformat:@ "passed number:%d", Parama];
    
    };
    
    
Myblock (2);   #pragma mark e.g.
    Block Create and Usage//basic Use return value UIImage object UIImage *oldimage=[uiimage imagenamed:@ "Avatar"];
    UIImage * (^myimageblock) (uiimage*) =^ (UIImage *image) {return image;
    };
    
    UIImage *newimage=myimageblock (oldimage);
    Basic use of no return value nsstring *string=@ "";
    void (^logblock) (NSString *) =^ (NSString *paramstr) {NSLog (@ "Logblock log:%@", paramstr);
    };
    
    Logblock (string);  Functions that contain block parameters are more common types, such as Successfulblock//method in network requests-(void) Turnnumberwithnumbera: (Nsinteger) a numberb: (Nsinteger) b
        Andmyblock: (void (^) (int aa,int bb)) myblock{int c=[[nsnumber Numberwithinteger:a]intvalue]; int D=[[nsnuMber Numberwithinteger:b]intvalue];
    Myblock (C,D);
        //Call [self turnnumberwithnumbera:1 numberb:2 andmyblock:^ (int aa, int bb) {NSLog (@ "AA is%d", AA);
    NSLog (@ "BB is%d", BB);
    
    
    }]; Block returns the value of TextField to a//b.h typedef void (^returntextblock) (NSString *showtext) when the value of two views Apush to b b disappears; Easy to invoke @interface B:uiviewcontroller @property (nonatomic, copy) Returntextblock returntextblock;//declares a B Lock Property-(void) Returntext: (returntextblock) block;//plus after easy to book a read the Block method @end//B.M-(void) R
    Eturntext: (returntextblock) block {//block Implementation method Self.returntextblock = Block; }-(void) Viewwilldisappear: (BOOL) Animated {if (Self.returntextblock!= nil) {Self.retu
    
    Rntextblock (Self.inputTF.text)//When the view will disappear call}//A.M call B *BVC = [[b alloc]init] when push is required; [BVC returntext:^ (NSString *showtext) {//define B view to invoke block SelF.showlabel.text = Showtext;
 }];


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.