ios - Assigning a variable from another class -
i have uiview uibutton created in 1 class: "viewclass". in mainvc class, called viewclas , needed call method in mainvc when button selected, created protocol. (i hope clear.)
here's how set protocol , delegate:
viewclass.h
@protocol viewclassdelegate -(void)buttonwasclicked; @end ... @property (nonatomic, strong) id<viewclassdelegate> delegate; viewclass.m
[submitbutton addtarget:self action:@selector(submitbuttontapped) forcontrolevents: uicontroleventtouchupinside]; - (void)submitbuttontapped { [self.delegate buttonwasclicked]; } mainvc.m
// imported , called delegate in mainvc.h. in .m set delegate -(void)buttonwasclicked { // perform action } is there way pass nsstring buttonwasclicked? don't mean this:
- (void)buttonwasclicked:(nsstring *)mystring because mystring has no value unless assign in mainvc method. want assign in submitbuttontapped (which in myview.m).
basically, want this:
- (void)textfielddidbeginediting:(uitextfield *)textfield in method, knows textfield without me defining when use method.
hope clear. if can explain better, feel free edit it. if need more clarification, please ask in comments.
viewclass.h
@protocol viewclassdelegate -(void)buttonwasclicked:(nsstring *)astring; @end viewclass.m
[submitbutton addtarget:self action:@selector(submitbuttontapped) forcontrolevents: uicontroleventtouchupinside]; - (void)submitbuttontapped { [self.delegate buttonwasclicked:@"this string"]; } mainvc.m
// imported , called delegate in mainvc.h. in .m set delegate -(void)buttonwasclicked:(nsstring *)astring { // astring = string }
Comments
Post a Comment