iOS Block learning

Source: Internet
Author: User

iOS block callback value, callback event,

Directly on the code

In FIRSTVC inside the TableView click Method, inside click Jump to another NEXTVC, and then return back after the FIRSTVC callback value

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Nextviewcontroller*next=[[Nextviewcontroller alloc]init]; __block Nextviewcontroller*_next =next;//Prevent circular references, so write __weaktypeof(self) _self =Self ; Next.back=^(intindex) {NSLog (@"--%d", index);//The value of the callback is printed out _next.data=@"ASDASDASD";//Pass aNSLog (@"index===%d", index);    [_self Openalter];    }; [Self presentviewcontroller:next animated:yes completion:null];}

Define block inside NEXTVC, as a @property value

void (^callback) (int  index); @interface nextviewcontroller:uiviewcontroller@property (nonatomic,assign) callBack back;

The Viewdidload method in NEXTVC calls this block:

Back(ten);

When clicked back, the value is recalled to the block method inside the FIRSTVC.

To accomplish this, block is an anonymous function, which is simpler than delegate, with values, callbacks,

Learn from http://my.oschina.net/leejan97/blog/268536

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.