IOS block use

Source: Internet
Author: User

aviewcontroller.h@interface aviewcontroller : uiviewcontroller {     uilabel *valuelabel;//. To receive the value returned by the Secondviewcontroller} @end implement aviewcontroller.m-  (void) viewdidload{     [super viewdidload];    valuelabel = [[uilabel alloc ] initwithframe:cgrectmake (10, 100, 300, 50)];    [self.view  Addsubview:valuelabel];    uibarbuttonitem *nextbtn = [[uibarbuttonitem  alloc] initwithtitle:@ "Next"                  style:uibarbuttonitemstyleplain target:self action: @selector (NextClick )];    self.navigationitem.rightbarbuttonitem = nextbtn;} -  (void) Nextclick{   bviewcontroller *svc = [[bviewcontroller alloc]  init];    svc.backvalue = ^ (Nsstring *strvalue)  {//set the Block property of Secondviewcontroller Edge, which is the key of this program      valueLabel.text = strValue;};     [self.navigationcontroller pushviewcontroller:svc animated:yes];} bviewcontroller.h@interface bviewcontroller : uiviewcontroller@property  (nonatomic,  copy)  void  (^backvalue) (Nsstring *strvalue), @property   (nonatomic, retain)   uitextfield *text; @end implement bviewcontroller.m@implementation bviewcontroller-  (void) viewDidLoad{     [super viewdidload];    self.view.backgroundcolor =  [UIColor whiteColor];    text = [[UITextField alloc]  Initwithframe:cgrectmake (10, 50, 300, 40)];    text.borderstyle =  UITextBorderStyleRoundedRect;    [self.view addSubview:text];     uibutton *btn = [uibutton buttonwithtype:uibuttontyperoundedrect];     Btn.frame = cgrectmake (10, 110, 60, 30);     [btn settitle:@ " Back to " forstate:uicontrolstatenormal];    [btn addtarget:self action: @selector (Btnclick)                       forcontrolevents:uicontroleventtouchupinside];    [self.view  ADDSUBVIEW:BTN];} -  (void) btnclick{self.backvalue (self.text.text);//Tune block Law [self.navigationcontroller  Popviewcontrolleranimated:yes];} @end


The idea of the block passing value:

1, a class to pass a value, that is, to establish the Block property void (^backvalue) (nsstring *strvalue), and then implement it: Self.backvalue (the value to pass )

2, Class B to get the value of a is to achieve the properties of a.

Aviewcontroller *AVC = [[Aviewcontroller alloc] init]; By block value, city switch avc.backvalue = ^ (NSString *strvalue) {//Set Secondviewcontroller? Side of the Block property, which is the key to this program Self.citynam    e = strvalue; };

3, parsing the Blcok attribute

Return value-Name-parameter

Return value-name-equals sign

@property (nonatomic, copy) void (^backvalue) (NSString *strvalue); @property (nonatomic, copy) NSString *name; =





IOS block use

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.