Saturday, 15 May 2010

ios - Passing UITextField Value to Function Within View -



ios - Passing UITextField Value to Function Within View -

i'm origin xcode , coding ios, have next problem. want add together function sets uitextfield values in viewcontroller based around value uistepper. code handles formatting 3 uitextfields, cutting 1 shorten example. code works fine:

- (ibaction)temp_stepper_changed:(uistepper *)sender { integer_t steppervalue = (integer_t) sender.value; nsstring *temp_format = [[nsstring alloc] initwithformat: @"%%.%df",steppervalue]; double fahrenheit = [_tempf_text.text doublevalue]; nsstring *fresultstring = [[nsstring alloc] initwithformat: temp_format,fahrenheit]; _tempf_text.text = fresultstring; }

i have several places want this, want create function call, , set function view controller's .m file:

void temp_text_update (double f_temp){ nsstring *fresultstring = [[nsstring alloc] initwithformat: @"%.2f",f_temp]; _tempf_text.text = fresultstring; }

the function won't compile, results in error:

use of undeclared identifier '_tempf_text'

without line, compiles fine, can phone call function, pass values, etc. had assumed (remember, origin @ this) uistepper had _tempf_text in it's scope, function beingness in same .m file well. there magic happening behind scenes allows ibaction type calls access value viewcontroller items, function missing said magic? i'll need uistepper value finish function. built using storyboard, control-drag outlets , actions, header file is:

@interface temperatureviewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uitextfield *tempf_text; @property (weak, nonatomic) iboutlet uistepper *temp_stepper; - (ibaction)tempf_cnvbutton:(id)sender; - (ibaction)temp_stepper_changed:(uistepper *)sender;

i've spent few hours searching, including site, found references 1 viewcontroller , forth, doesn't match; tried few things anyway, nil worked (though yielded errors). suspect obvious , simple not asked, i've run out of ideas , help appreciated.

to reply specific question, right syntax signature you're trying write this:

- (void)temp_text_update:(double)f_temp {

this says have method named temp_text_update: takes double parameter called f_temp , doesn't homecoming anything.

a more general solution utilize nsnumberformatter go between strings , doubles in locale sensitive way, this:

// double string - (double)doublefromstring:(nsstring *)string { nsnumberformatter *formatter = [[nsnumberformatter alloc] init]; [formatter setnumberstyle:nsnumberformatterdecimalstyle]; nsnumber *number = [formatter numberfromstring:string]; homecoming [number doublevalue]; } // string double: - (nsstring *)stringfromdouble:(double)adouble { nsnumberformatter *formatter = [[nsnumberformatter alloc] init]; [formatter setnumberstyle:nsnumberformatterdecimalstyle]; homecoming [formatter stringfromnumber:[nsnumber numberwithdouble:adouble]]; }

then computing functions this:

nsstring *text = self.somecontrolview.text; double adouble = [self doublefromstring:text]; // computation on adouble, resulting in double result = // whatever [self setoutputtextwithresult:result];

finally, method (better named):

- (void) setoutputtextwithresult:(double)result { self.somecontrolview.text = [self stringfromdouble:result]; }

it's short now, doesn't need it's own method.

ios function uitextfield scope

No comments:

Post a Comment