ios - UISlider value is zero when trying to pass value to UIView with custom class -


i have uiview, uilabel, , uislider placed in uiveiwcontroller in viewcontroller files. have programmatically added uilabel uiview debugging tool.

yes, connect uilabel within uiview same have other uilabel once solve problem passing value in graph calculation. i'm open suggestion passing variable uiview prefer avoid creating global variables. i'm passing uislider value uilabel placed in uiveiwcontroller should when try send value "referencing" slider value in graphview.m implementation file never makes , i'm getting value of zero.

i've spent 2 days scouring stackoverflow forums, reviewing documentation, , re-reading books trying figure out issue , while i've learned great deal (and fixed other problems) i've still not figured 1 out. i'm sure simple , i'm sure gonna kick myself. welcome references other documentation (i've read it) please offer constructive feedback , direction well. code below:

viewcontroller.h

#import <uikit/uikit.h>  @interface viewcontroller : uiviewcontroller {     nsstring *viewcontrollerslidervalue; } - (nsstring *) viewconrtollerslidervalue;  @property (weak, nonatomic) iboutlet uilabel *slidervaluelabel;  @property (weak, nonatomic) iboutlet uislider *slidervalue;  - (ibaction)sliderchange:(uislider *)sender;  @end 

viewcontroller.m

#import "viewcontroller.h" #import "graphview.h"  @interface viewcontroller ()  @end  @implementation viewcontroller  - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.      float sliderval = self.slidervalue.value;     self.slidervaluelabel.text = [nsstring stringwithformat:@"%.1f",sliderval];     nslog(@"viewdidload: sliderval = %f", sliderval); }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  - (ibaction)sliderchange:(uislider *)sender {     float sliderval = sender.value;     self.slidervaluelabel.text = [nsstring stringwithformat:@"%.1f", sliderval];     nslog(@"sliderchange: sliderval = %f", sliderval);  }  - (nsstring *)viewconrtollerslidervalue {     float sliderval = self.slidervalue.value;     viewcontrollerslidervalue = [nsstring stringwithformat:@"%f",[self slidervalue].value];     nslog(@"sliderchange: sliderval = %f", sliderval);     return viewcontrollerslidervalue; }  @end 

graphview.h

#import <uikit/uikit.h>  @interface graphview : uiview  @end 

graphview.m

#import "graphview.h" #import "viewcontroller.h"  @implementation graphview  - (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         // initialization code     }     return self; }  - (void)drawrect:(cgrect)rect {     viewcontroller *viewcontroller = [[viewcontroller alloc] init];      uilabel *mylabel = [[uilabel alloc] initwithframe:cgrectmake(30, 90, 140, 20)];     nslog(@"graphview: drawrect: viercontrllerslidervalue = %@", [viewcontroller viewconrtollerslidervalue]);     mylabel.text = [viewcontroller viewconrtollerslidervalue];     mylabel.backgroundcolor = [uicolor lightgraycolor];     mylabel.textalignment = nstextalignmentcenter;     [self addsubview:mylabel]; }  @end 

the problem graphview creating new view controller when calls viewcontroller *viewcontroller = [[viewcontroller alloc] init];, separate 1 being displayed on screen. new controller's slider isn't 1 you're manipulating, it's never changing values.

an additional problem graphview adding new label every time call drawrect:. method called every time view needs re-drawn. instead, add label subview in -initwithframe: method, , keep reference instance variable or property.

it better graphview never know view controller @ all. instead, graphview should have setter method set value should displayed. controller should responsible telling graph view update whenever slider changes. this:

@interface graphview @property (nonatomic) float graphvalue; @end  @implementation graphview {     uilabel *graphlabel; }   - (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         graphlabel = [[uilabel alloc] initwithframe:cgrectmake(30, 90, 140, 20)];         graphlabel.backgroundcolor = [uicolor lightgraycolor];         graphlabel.textalignment = nstextalignmentcenter;         [self addsubview:graphlabel];     }     return self; }   - (void)setgraphvalue:(float)thevalue {     _graphvalue = thevalue;     graphlabel.text = [nsstring stringwithformat:@"%f", thevalue] }  // note; no need -drawrect, because drawn label, subview.  @end 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -