Can anyone explain this to me please (I’m getting so confused)
i have a button i click the button to call a method (flowCalculaton)
i set a local float var assign a NSTextField to the var to get its contents then retune the value ,
ALl builds and runs fine but i get a value of 0 to the answerText (label)
but if i change float setVolume = 233 ; for example i get 233 shown in the label
- (IBAction)calculate:(id)sender {
AppControls *cal =[[AppControls alloc]init];
float callMethod = [cal flowCalculation] ;
[answerText setFloatValue: callMethod ];
[cal release];
}
- (float) flowCalculation {
float setVolume = [volumeText floatValue];
return setVolume ;
}
if i do this the same call in (flowCalculation) to volumeText gives me the NSTextField value.
- (IBAction)calculate:(id)sender {
//AppControls *cal =[[AppControls alloc]init];
//float callMethod = [cal flowCalculation] ;
[answerText setFloatValue: [volumeText floatValue] ];
// [cal release];
}
Why is it not assigning [volumeText floatValue] to the var within a method please ?
Help appreciated.
What is
AppControlsand why are you allocating a new instance to do the calculation?What is
volumeTextinAppControls, and what would be its value in a newly allocated instance ofAppControls?I’m just guessing, but maybe you meant to do this:
EDIT in reply to comments:
I’m guessing that
AppControlsis your view controller. So you have an instance of it being displayed to the user, and the user is editing the contents of the text fields in that particular instance ofAppControls. When you callAppControls *cal =[[AppControls alloc]init];, you’re creating a brand new, empty, instance ofAppControlsthat the user can’t even see. So when you try to get the values of the text fields in that new instance, they are empty, because that is not the instance that the user was editing.