uislider - Linking stepper to Slider UIObject in C4 -


i new c4 please gentle...

if want link slider value label done nsstring stringwithformat... e.g.:

self.mylabel.text = [nsstring stringwithformat:@"%4.2f",slider.value]; 

i added stepper object well, , updates mylabel:

self.mylabel.text = [nsstring stringwithformat:@"%4.2f",stepper.value]; 

but intuitive if slider position follows label value when i'm using stepper. .value not available property in uilabel... how take mylabel.text property , push slider.value property without getting datatype mismatch error?

this question has 2 answers, how using c4 objects , how interface builder / uicontrols. i'll show both ways, ui first can compare c4 way afterwards.

uicontrol


to uicontrols first set c4workspace.h header has following methods , properties:

@property (assign, nonatomic) iboutlet uilabel *mylabel; @property (assign, nonatomic) iboutlet uislider *myslider; @property (assign, nonatomic) iboutlet uistepper *mystepper;  -(ibaction)sliderwasupdated:(uislider *)slider; -(ibaction)stepperwasupdated:(uistepper *)stepper; 

then, in drag 3 components onto projects xib file (i.e. uislider, uilabel , uistepper). link action sliderwasupdated: slider using valuechanged option, , stepperwasupdated: action stepper using valuechanged option. step selecting c4canvas.xib project right-clicking on yellow cube, dragging actions listed in pop-up menu each of objects placed on canvas.

next, add following code c4workspace.m file:

@implementation c4workspace  -(void)setup {     self.mystepper.minimumvalue = 0.0f;     self.mystepper.maximumvalue = 10.0f;      self.myslider.minimumvalue = 0.0f;     self.myslider.maximumvalue = 10.0f; }  -(ibaction)sliderwasupdated:(uislider *)slider {     slider.value = [c4math round:slider.value];     self.mylabel.text = [nsstring stringwithformat:@"%4.2f",slider.value];     self.mystepper.value = slider.value;     [self.mylabel sizetofit]; }  -(ibaction)stepperwasupdated:(uistepper *)stepper {     self.mylabel.text = [nsstring stringwithformat:@"%4.2f",stepper.value];     self.myslider.value = stepper.value;     [self.mylabel sizetofit]; }  @end 

in setup make sure min/max values of both ui objects same (so can keep them matched up).

in stepperwaschanged: method 2 things:

  1. we use stepper's value set label's text
  2. we use stepper's value set slider's value!

in sliderwaschanged: method same thing, updating stepper, round value of slider increments in steps (just keep things tidy).

c4control


to same c4 objects, instead of native ui objects, set things little differently. first, don't add our c4canvas.xib, instead we'll set objects manually.

in c4workspace.h file, add following lines of code:

@property (readwrite, nonatomic, strong) c4label *mylabel; @property (readwrite, nonatomic, strong) c4slider *myslider; @property (readwrite, nonatomic, strong) c4stepper *mystepper;  -(void)sliderwasupdated:(c4slider *)slider; -(void)stepperwasupdated:(c4stepper *)stepper; 

notice of same except we're using c4 instead of ui prefixes. also, call our methods -(void) instead of -(ibaction) because we're not using interface builder.

next, add following code c4workspace.m:

@implementation c4workspace  -(void)setup {     [self createaddobjects];     //calibrate min/max values     self.mystepper.minimumvalue = 0.0f;     self.mystepper.maximumvalue = 10.0f;      self.myslider.minimumvalue = 0.0f;     self.myslider.maximumvalue = 10.0f; }  -(void)sliderwasupdated:(c4slider *)slider {     slider.value = [c4math round:slider.value];     self.mylabel.text = [nsstring stringwithformat:@"%4.2f",slider.value];     self.mystepper.value = slider.value;     [self.mylabel sizetofit]; }  -(void)stepperwasupdated:(c4stepper *)stepper {     self.mylabel.text = [nsstring stringwithformat:@"%4.2f",stepper.value];     self.myslider.value = stepper.value;     [self.mylabel sizetofit]; }  -(void)createaddobjects {     //set objects     self.mylabel = [c4label labelwithtext:@"values"];     self.mystepper = [c4stepper stepper];     self.myslider = [c4slider slider:cgrectmake(0, 0, 192, 44)];      //position them     cgpoint centerpoint = cgpointmake(self.canvas.center.x,                                       self.canvas.center.y - 100);     self.mystepper.center = centerpoint;      centerpoint.y += 100;     self.mylabel.center = self.canvas.center;      centerpoint.y += 100;     self.myslider.center = centerpoint;      //set action bindings     [self.myslider runmethod:@"sliderwasupdated:"                       target:self                     forevent:valuechanged];     [self.mystepper runmethod:@"stepperwasupdated:"                        target:self                      forevent:valuechanged];      [self.canvas addobjects:@[self.mystepper,self.mylabel,self.myslider]]; }  @end 

differences


the major difference between 2 approaches whether or not use interface builder. in c4 approach need add method called createaddobjects our project our slider, label , stepper added canvas.

this method contains methods binding actions of our c4uielements our code, happens in lines:

[self.myslider runmethod:@"sliderwasupdated:"                   target:self                 forevent:valuechanged]; [self.mystepper runmethod:@"stepperwasupdated:"                    target:self                  forevent:valuechanged]; 

once these set only difference specifying use of c4 objects instead of ui objects, like:

-(void)sliderwasupdated:(c4slider *)slider {...} 

instead of

-(ibaction)sliderwasupdated:(uislider *)slider {...} 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -