objective c - Custom delegate does not working in iOS -
my custom delegate not work. used custom delegate time it's not working. want pass data (uislider
value after change) uiviewcontroller
class subclass of uiview
. here code. please me out. -
ratingviewcontroller.h -
#import <uikit/uikit.h> @class starratingview; //define class, protocol can see class @protocol delegateforslider <nsobject> //define delegate protocol - (void) getvalue:(cgfloat) value; //define delegate method implemented within class @end @interface ratingviewcontroller : uiviewcontroller @property (nonatomic, weak) id <delegateforslider> delegate; //define delegateforslider delegate @property (weak, nonatomic) iboutlet uiview *ratingview; - (ibaction)slidervaluechange:(id)sender; @property (weak, nonatomic) iboutlet uislider *slider; @end
ratingviewcontroller.m -
#import "ratingviewcontroller.h" #import "starratingview.h" @interface ratingviewcontroller () @end @implementation ratingviewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view. starratingview* starviewwithanimated = [[starratingview alloc]initwithframe:cgrectmake(5, 8, 100, 50) andrating:49]; [self.ratingview addsubview:starviewwithanimated]; } - (ibaction)slidervaluechange:(id)sender { nslog(@"value changed"); [self.delegate getvalue:self.slider.value]; } @end
and in class want uislider
value. starratingview.h -
#import <uikit/uikit.h> #import "ratingviewcontroller.h" @interface starratingview : uiview <delegateforslider> - (id)initwithframe:(cgrect)frame andrating:(int)rating; @end
starratingview.m -
#import "starratingview.h" @interface starratingview() @property (strong, nonatomic) ratingviewcontroller *ratingviewcontroller; @property (nonatomic, strong) uilabel* label; @end @implementation starratingview - (id)initwithframe:(cgrect)frame andrating:(int)rating { self = [super initwithframe:frame]; if (self) { _ratingviewcontroller.delegate = self; //add label after star view show rating percentage self.label = [[uilabel alloc]initwithframe:cgrectmake(0, 0,frame.size.width, frame.size.height)]; self.label.font = [uifont systemfontofsize:18.0f]; self.label.text = [nsstring stringwithformat:@"%d%%",rating]; self.label.textalignment = nstextalignmentright; self.label.textcolor = [uicolor whitecolor]; self.label.backgroundcolor = [uicolor bluecolor]; [self addsubview:self.label]; } return self; } -(void) getvalue:(cgfloat)value { nslog(@"got value : %f", value);//this line print nothing, mean never fire } @end
you need set delegate in ratingviewcontroller.m
's viewdidload
method. delegate isn't being set because when initwithframe:andrating:
called, _ratingviewcontroller
nil
.
also, bad practice have reference uiviewcontroller
in uiview
. not create retain cycle, ignores half reason of using delegates: add flexibility object conforms delegateforslider
can set starratingview
's delegate
property.
for more information on delegation check out this programming overflow post , apple's docs
Comments
Post a Comment