this question regarding delegation, trying pass data child view controller linked container view has picker view. asked related question before , kind enough reply following code:
"i'm going explain example how delegation works.
edit childviewcontroller.h this:"
@protocol childviewcontrollerdelegate; @interface childviewcontroller : uiviewcontroller @property (weak)id <childviewcontrollerdelegate> delegate; @end @protocol childviewcontrollerdelegate <nsobject > - (void) passvalue:(uicolor *) thevalue; @end
"on childviewcontroller.m when want pass parentviewcontroller , this:"
- (void) mymethod { [delegate passvalue:[uicolor redcolor]]; // pass value here }
on parentviewcontroller.h
#import "childviewcontroller.h" @interface parentviewcontroller : uiviewcontroller <childviewcontrollerdelegate > // adopt protocol { }
on parentviewcontroller.m:
- (void) passvalue:(uicolor *) thevalue { //here receive color passed childviewcontroller }
now careful. work if set delegate. when declare childviewcontroller inside parentviewcontroller class, this:
childviewcontroller * controller = [[childviewcontroller alloc]initwithinibname:@"childviewcontroller"]; controller.delegate = self; //without line code won't work!"
anyway, helped me quite bit , 1 of helpful pieces of advice have got yet. unfortunately, 1 of lines in code returns error , making delegate nil. one:
childviewcontroller * controller = [[childviewcontroller alloc]initwithinibname:@"childviewcontroller"];
the error is: "no visible @interface 'childviewcontroller' declares selector 'initwithnibname:'
as following instructions on trying learn, bit lost. begin with, when try type it, proposed method takes argument: [[childviewcontroller alloc]initwithnibname:<#(nullable nsstring *)#> bundle:<#(nullable nsbundle *)#>];
i have done nslog [self.delegate description] , it's come null. can help? thanks
it sounds delegation advice good, left off parameter building view controller via storyboard.
childviewcontroller *controller = [[childviewcontroller alloc] initwithnibname:@"childviewcontroller" bundle:[nsbundle mainbundle]];
...will work, if there's interface builder file in project called "childviewcontroller".
Comments
Post a Comment