The Block for iOS development

Source: Internet
Author: User

The meaning and role of iOS development Block1.block

UI Development and network common function implementation callback, button event handling method is callback method, after the network download callback processing

(1) button target-action a method in the incoming button

(2) Table view passes in a pointer to self, callback method in the View controller

(3) block statement blocks, resolving callbacks, understood as "anonymous functions", defined in the method

Basic use of 2.block (syntax)

Knowledge points involved:

Defines a block variable, defining block statement blocks

Block parameters and return values

Block captures external variables (including self)

button

Block basic use-(void) blockbasicuse{//block Understanding anonymous function//void func ()//{//}//1.block variable definition//Skill: Resolution        The problem of the law is difficult to write/void Func ();        Define the block variable, ^ to define block,//tip: Parentheses around the function name, preceded by the functions name in the ^ void (^block) ();    Defines block statement blocks that are stored in the block variable block = ^void () {NSLog (@ "I am Block");        };        Execute block ();        2. With parameters and return value block//instance implementation calculation two number of the sum block//int myadd (int x,int y);    Int (^myadd) (int x,int y) = ^int (int x,int y) {return x+y;        };    int s = myadd (10,20);            NSLog (@ "s =%d", s);    3.block capturing external external variables//blocks using variables outside the block note//int num = 10;    __block int val = 100;        void (^B1) () = ^void () {//can use and modify instance variables _page = 1;                The value of local variables cannot be modified in block//num++;                The local variable val++ of __block modification can be modified in block; There may be warnings due to memory problems, note//__weak typeof (self) weakself = self;        Block outside definition//Weakself.url = @ "text";    Self.url = @ "TXT";    };B1 ();} 
3.block application in development (OC,UI, Network) using block for sorting
Use//1.nsmutablearray in OC to    sort        Dog *ahua = [[Dog alloc] init];    Ahua.nickname = @ "Ahua";    Ahua.age = 4;        Dog *amiao = [[Dog alloc] init];    Amiao.nickname = @ "Amiao";    Amiao.age = 3;        Dog *dahuang = [[Dog alloc] init];    Dahuang.nickname = @ "Dahuang";    Dahuang.age = 5;        Nsmutablearray *marr = [[Nsmutablearray alloc] Initwitharray:@[ahua,amiao,dahuang]];    Marr sortusingselector:<# (SEL) #>    [Marr Sortusingcomparator:^nscomparisonresult (ID obj1, id obj2) {        Dog *adog = obj1;        Dog *bdog = obj2;        return adog.age > Bdog.age;        return [Adog.nickname Compare:bDog.nickname] < 0;    }];        For (Dog *d in Marr) {        NSLog (@ "name =%@ age=%d", d.nickname,d.age);    }
Animating with block
2.UIView animation    UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (ten, +, +)];    Label.text = @ "I am the label";    Label.backgroundcolor = [Uicolor redcolor];    [Self.view Addsubview:label];        Move Down    //[uiview animatewithduration:<# (nstimeinterval) #> animations:<#^ (void) animations#>];    [UIView animatewithduration:2 animations:^{        cgrect frame = label.frame;        Frame.origin.x + = 280;        Label.frame = frame;    } completion:^ (BOOL finished) {        NSLog (@ "End of animation");                [UIView animatewithduration:1 animations:^{            label.transform = cgaffinetransformmakerotation (M_PI);        } completion:^ (BOOL finished) {                }];            }];

Using block to implement the interface transfer value

If there are two interface a interface, B interface, a interface to create a B interface, B boundary value passed to a interface

A interface setting block

-(void) Btnclick: (UIButton *) button{    Secondviewcontroller *svc = [[Secondviewcontroller alloc] init];        Set Block    [Svc setchangebackgroundcolor:^ (NSString *color) {        if ([Color isequaltostring:@ "Blue"])        {            Self.view.backgroundColor = [Uicolor bluecolor];        }    ];            [Self presentviewcontroller:svc animated:yes completion:nil];}

  

  

The Block for iOS development

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.