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: