Use block to send value in iOS

Source: Internet
Author: User
Tags uikit

Transferred from: http://blog.sina.com.cn/s/blog_60b45f230100yiaf.html

Using this method to pass a value can replace the delegate. Specific examples:

MainView.h
#import <UIKit/UIKit.h>@interface  mainview:uiviewcontroller{    iboutlet UIButton * btn;    Iboutlet UILabel* labshow;} -(Ibaction) Push: (ID) sender; @end

mainview.m

#import "MainView.h"#import "SecondView.h"@implementationMainView- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{ Self=[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if(self) {}returnSelf ;}- (void) didreceivememorywarning{[Super didreceivememorywarning];}#pragmaMark-view lifecycle-(void) viewdidload{[Super Viewdidload];}-(Ibaction) Push: (ID) sender{Secondview*s = [[Secondview alloc] initwithblock:^ (NSString *str) {NSLog (@"%@", str); Labshow.text=str;    }];    [Self.navigationcontroller pushviewcontroller:s Animated:yes]; [s release];}- (void) viewdidunload{[Super Viewdidunload];}-(BOOL) shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation{return(Interfaceorientation = =uiinterfaceorientationportrait);}@end

SecondView.h
#import <UIKit/UIKit.h>void (^myblock) (NSString *); @interface secondview:uiviewcontroller{    iboutlet uitextfield* Txtview;    Myblock my;} -(Ibaction) Back: (ID) sender; -(ID) initwithblock: (myblock) str; @end

secondview.m
#import "SecondView.h"@implementationSecondview-(ID) Initwithblock: (myblock) str{ Self=[Super Init]; if(self) {my=str; }    returnSelf ;}-(Ibaction) Back: (ID) sender{NSString* s =Txtview.text; if(my) {my (s); } [Self.navigationcontroller Popviewcontrolleranimated:yes];}- (void) didreceivememorywarning{[Super didreceivememorywarning];}-(void) dealloc{block_release (my);    [Super Dealloc]; }#pragmaMark-view lifecycle-(void) viewdidload{[Super Viewdidload];}- (void) viewdidunload{[Super Viewdidunload];}-(BOOL) shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation{return(Interfaceorientation = =uiinterfaceorientationportrait);}@end

Interface:




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.