IOS Block Callback

Source: Internet
Author: User

Description: This article is an application based on understanding block fundamentals, assuming that the reader already has the block basics.

Objective: To pass the value of Secondviewcontroller into Viewcontroller in block callback mode, and at some time, through block callback, can avoid delegate cumbersome

1, new single View Application project, new Secondviewcontroller

2, add Uilabel in Viewcontroller to display the value passed by Secondviewcontroller, set label to member variable, add a UIButton, Used to click Jump to Secondviewcontroller

@interfaceViewcontroller () {UILabel*_label;}@end- (void) viewdidload {[Super viewdidload]; //add UIButton to jump to SecondviewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Tosecondviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Graycolor];    [Btn addtarget:self Action: @selector (Tosecondviewcontroller) forcontrolevents:uicontroleventtouchupinside];        [Self.view ADDSUBVIEW:BTN]; //add Uilabel to show values passed by Secondviewcontroller_label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _label.backgroundcolor=[Uicolor Yellowcolor]; [Self.view Addsubview:_label];}

3, add a Uitextfield to the SECONDVIEWCONTROLLER.M, set the member variable, enter the value, add a UIButton, click to jump back Viewcontroller

@interfaceSecondviewcontroller () {Uitextfield*_textfield;}@end- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; //Add a button to return ViewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Backtoviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Bluecolor];    [Btn addtarget:self Action: @selector (Backtoviewcontroller) forcontrolevents:uicontroleventtouchupinside];        [Self.view ADDSUBVIEW:BTN]; //Add a Uitextfield_textfield = [[Uitextfield alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _textfield.backgroundcolor=[Uicolor Greencolor]; [Self.view Addsubview:_textfield];}

4, in SecondViewController.h, use typedef to define a block and set it to a property, and declare a callback method

#import <UIKit/UIKit.h>// define a blockvoid (^returnvalueblock) (NSString * value); @interface Secondviewcontroller:uiviewcontroller@property (nonatomic,copy) returnvalueblock ReturnValueBlock; // callback Method -(void) returnvalue: (returnvalueblock) block; @end

5, implement the callback method, and call when you click the Return viewcontroller button

-(void) backtoviewcontroller {    // when clicking Back to Viewcontroller, callback    if ( Self.returnvalueblock! = nil) {        self.returnvalueblock (_textfield.text);    }        [Self Dismissviewcontrolleranimated:yes completion:nil];} -(void) returnvalue: (returnvalueblock) block {    = Block;}

6, use the Block method in VIEWCONTROLLER.M to assign a value to the label

-(void) tosecondviewcontroller {    *second = [[Secondviewcontroller alloc] init];    [Second returnvalue:^ (NSString *value) {        = value;    }];    [Self Presentviewcontroller:second animated:yes completion:nil];}

Source Address: Https://github.com/rokistar/PassValueUsingBlock

IOS Block Callback

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.