////ZYViewController.h//blocktest////Created by Yejiong on 14/11/2.//copyright©2014 years zzz. All rights reserved.//#import<UIKit/UIKit.h>//1. Declare a delegate agreement. @protocolZyviewcontrollerdelegate <NSObject>//declares a method used to pass a value. //The return value indicates the result of the pass value, and if you do not care then write void. //the parameters in the method are the values we need to pass, and if you want to pass multiple values, continue appending the arguments. - (void) Selectedcolor: (uicolor*) color;@end@interfaceZyviewcontroller:uiviewcontroller//2. Declare a delegate property. @property (nonatomic, assign)ID<ZYViewControllerDelegate>Delegate;@end
////ZYVIEWCONTROLLER.M//blocktest////Created by wanglixing on 14/11/2.//copyright©2014 years zzz. All rights reserved.//#import "ZYViewController.h"@interfaceZyviewcontroller () @property (nonatomic, retain) Nsarray*colors;@end@implementationZyviewcontroller- (void) Dealloc {[_colors release]; [Super Dealloc];}- (void) viewdidload {self.view.backgroundColor=[Uicolor Whitecolor]; //The array contains the headings for all the options. uisegmentedcontrol* sc = [[Uisegmentedcontrol alloc] initwithitems:@[@"Red",@"Green",@"Blue"]]; Sc.frame= CGRectMake ( -, -, $, -); //using uicontroleventvaluechanged[SC addtarget:self action: @selector (valuechanged:) forcontrolevents:uicontroleventvaluechanged]; [Self.view ADDSUBVIEW:SC]; [SC release]; Self.colors=@[[uicolor Redcolor], [Uicolor Greencolor], [Uicolor Bluecolor]];}- (void) ValueChanged: (uisegmentedcontrol*) Sender {NSLog (@"%@", @ (Sender.selectedsegmentindex)); //3. Use the delegate object to invoke the protocol method to pass the value. if([_delegate respondstoselector: @selector (selectedcolor:)]) {//before calling a method, you need to determine whether delegate implements the method. [_delegate Selectedcolor:_colors[sender.selectedsegmentindex]]; }}@end
Implementing a Proxy
////VIEWCONTROLLER.M//blocktest////Created by Yejiong on 14/11/2.//copyright©2014 years zzz. All rights reserved.//#import "ViewController.h"#import "ZYViewController.h"@interfaceViewcontroller () <ZYViewControllerDelegate>@end@implementationViewcontroller- (void) viewdidload {self.view.backgroundColor=[Uicolor Whitecolor];}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{Zyviewcontroller* VC =[[Zyviewcontroller alloc] init]; 5. SetDelegateproperty. VC.Delegate=Self ; [Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes]; [VC release]; }#pragmaMark-zyviewcontrollerdelegate//4. Implement the Protocol method to obtain the passed value. - (void) Selectedcolor: (Uicolor *) Color {self.view.backgroundColor=color;}@end
Use block to pass value
////ZYBlockViewController.h//blocktest////Created by Yejiong on 14/11/2.//copyright©2014 years zzz. All rights reserved.//#import<UIKit/UIKit.h>//use block to pass the value into 4 steps. //1. Declare a block type, the return value indicates the result of the value, and the parameter indicates the data of the value passed. typedefvoid(^block) (uicolor*color);@interfaceZyblockviewcontroller:uiviewcontroller//2. Declare a block property. @property (nonatomic, copy) block block; @property (nonatomic, retain) Uicolor*Selectedcolor;@end
////ZYBLOCKVIEWCONTROLLER.M//blocktest////Created by Yejiong on 14/11/2.//copyright©2014 years zzz. All rights reserved.//#import "ZYBlockViewController.h"@interfaceZyblockviewcontroller () @property (nonatomic, retain) Nsarray*colors;@end@implementationZyblockviewcontroller- (void) Dealloc {[_colors release]; [_block release]; [_selectedcolor release]; [Super Dealloc];}- (void) viewdidload {self.view.backgroundColor=[Uicolor Whitecolor]; //The array contains the headings for all the options. uisegmentedcontrol* sc = [[Uisegmentedcontrol alloc] initwithitems:@[@"Red",@"Green",@"Blue"]]; Sc.frame= CGRectMake ( -, -, $, -); //using uicontroleventvaluechanged[SC addtarget:self action: @selector (valuechanged:) forcontrolevents:uicontroleventvaluechanged]; [Self.view ADDSUBVIEW:SC]; [SC release]; Self.colors=@[[uicolor Redcolor], [Uicolor Greencolor], [Uicolor Bluecolor]]; Nsinteger Index=[_colors Indexofobject:_selectedcolor]; if(Index! =nsnotfound) {Sc.selectedsegmentindex=index; }}- (void) ValueChanged: (uisegmentedcontrol*) Sender {NSLog (@"%@", @ (Sender.selectedsegmentindex)); //3. Call block to pass the value. if(_block) {//before calling block, you need to determine if the block is implemented. _block (_colors[sender.selectedsegmentindex]); }}@end
////VIEWCONTROLLER.M//blocktest////Created by wanglixing on 14/11/2.//copyright©2014 years zzz. All rights reserved.//#import "ViewController.h"#import "ZYBlockViewController.h"@interfaceViewcontroller () <ZYViewControllerDelegate>@end@implementationViewcontroller- (void) viewdidload {self.view.backgroundColor=[Uicolor Whitecolor];}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{Zyblockviewcontroller* VC =[[Zyblockviewcontroller alloc] init]; //4. Implement the block and get the value passed in. Vc.block = ^ (uicolor*color) { //use color. Self.view.backgroundColor =color; //written before push, the effect is the same after push, because it is the same uinavigationcontroller//[Self.navigationcontroller Popviewcontrolleranimated:yes]; }; Vc.selectedcolor=Self.view.backgroundColor; [Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes]; [VC release];}@end
Use of proxy and block transfer values