Block of iOS

Source: Internet
Author: User

iOS Block1.block

Callback (handle event of the button, callback processing after network download)

(1) button Target-action method in the incoming button

(2) Table view incoming self pointer

(3) block statement blocks, resolving callbacks

2.block statements
    //definition of 1.block variables//tip: Grammatical weird//defines block--like void (*func) (); function pointer    void(^block)        (); //defining statement blocks ()block = ^void(){//define block statement blocks--Save to block variablesNSLog (@"I am a block");    };   Block (); //Execution//2. Eg: Calculates a sum of two and int myadd (int a,int b);    int(^myadd) (intAintb) = ^int(intAintb) {            returnA +b;    }; intsum = Myadd (Ten, -); NSLog (@"sum =%d", sum); //3.block captures external variables--cannot modify the values of local variables, you can use and modify instance variables//__block int A; (external variable)//int num = ten;__blockintValue = -; void(^B1) () = ^void() {_page=1; Value++;//num++; //Error//there may be warnings due to memory problems, note://__weak typeof (self) weakself = self; //block outside definition//Weakself.url = @ "text";Self.url =@"text";//memory problems caused by mutual references    }; B1 ();
3.block Applications (1) Nsmutablearray sorting
#import " Dog.h "
//OC Applications//1.NSMutableArray SortingDog *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*muarr =[[Nsmutablearray alloc] Initwitharray:@[ahua,amiao,dahuang]; [Muarr sortusingcomparator:^nscomparisonresult (IDObj1,IDobj2) {Dog*adog =obj1; Dog*bdog =Obj2; //return adog.age > bdog.age;        return[Adog.nickname Compare:bdog.nickName];//Press Nickname Row    }];  for(Dog *doginchMuarr) {NSLog (@"name =%@,age =%d", Dog.nickname,dog.age); }
(2) UIView animation
 //2.UIView AnimationUILabel *label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Label.text=@"I am a label"; Label.backgroundcolor=[Uicolor Redcolor];        [Self.view Addsubview:label]; [UIView animatewithduration:2animations:^{cgrect rect=Label.frame; RECT.ORIGIN.Y+= $; Label.frame=rect; } Completion:^ (BOOL finished) {//can be nestedNSLog (@"End Animation"); [UIView animatewithduration:2animations:^{label.transform= Cgaffinetransformmakerotation (M_PI);//Rotation} completion:^(BOOL finished) {}]; }];
(3) Reverse Transfer value
#import <UIKit/UIKit.h>@interface  secondviewcontroller:uiviewcontroller//  void Action (NSString *color); // void (^action) (NSString *color); -(void) Setchangebackgroundcolor: (void(^) (NSString *color)) action; @end
#pragmaMark-(void) Setchangebackgroundcolor: (void(^) (NSString *)) action{_action=Action;}#pragmaMark--(void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view. //Reverse pass -through valueUIButton *btn =[UIButton Buttonwithtype:uibuttontyperoundedrect]; Btn.frame= CGRectMake ( -, $, -, -);//according to the actual adjustment[BTN Settitle:@"return"Forstate:uicontrolstatenormal];    [Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];    [Self.view ADDSUBVIEW:BTN]; }-(void) Btnclick: (UIButton *) btn{if(_action) {_action (@"Blue");//Reverse Pass Value} [self Dismissviewcontrolleranimated:yes completion:nil];}

In the first Viewconreoller receive

-(void) Btnclick: (UIButton *) btn{    *svc = [[Secondviewcontroller alloc] init];         = [Uicolor yellowcolor];    [svc Setchangebackgroundcolor:^ (NSString *color) {        if([color isequaltostring:@ "  Blue"]) {                    = [Uicolor bluecolor];        }    }];        [Self presentviewcontroller:svc animated:yes completion:nil];}

Block of 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.