I'm hitting a bit of a brick wall in getting a program to work in Objective C. In fact, I've been struggling with this problem all day.
I have a ViewController which takes input from a view, which will contain a float variable (weight). I'll need to use this variable in other parts of my program, and therefore I've assigned the data to a very basic class, as follows:
//patient.h
#import <Foundation/Foundation.h>
@interface Patient : NSObject
@property float weight;
@end
and the .m:
//patient.m
#import "Patient.h"
@implementation Patient
float weight;
@end
My first view controller: -There's nothing of note in the .h file aside from connections to UI elements, and:
#import "Patient.h"
In the interests of clarity, I've omitted the code for simply servicing the UIview and getting the user inputs
//demographicsViewController.m
#import "demographicsViewController.h"
@interface demographicsViewController ()
{
// declares an array, for the custom picker
NSArray *_agePickerArray;
NSArray *_ageWeightArray;
}
@end
@implementation demographicsViewController
/* (omitting a sizeable chunk of code in the interests of clarity. Please let me know if you think it important to see) */
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//captures the output on the text field and converts it into a float that can be slotted straight in to the model. Sets a default value if the field is blank
float weightTextFieldInterrogation;
if([[_weightDisplayTextBox text] length] != 0)
{
weightTextFieldInterrogation = [_weightDisplayTextBox.text floatValue];
}
else
{
weightTextFieldInterrogation = 70.0;
}
//Takes the weight value and plumbs it into the central weight database ready to be used in other parts of the app
NSLog(@"weight: %.1f",weightTextFieldInterrogation);
[Patient.weight] = weightTextFieldInterrogation;
Patient* thePatient = [[Patient alloc]init];
[thePatient setWeight:weightTextFieldInterrogation];
NSLog(@"Stored weight: %.1f",thePatient.weight);
}
@end
The problem I'm having is that when I try to look up the value I've stored for weight or thePatient.weight, the variable is either unrecognised (despite #import "patient" and #import "demographicsViewController), and if I instantiate it again, then the value is blank.
I've read up on a number of different solutions to this, but have been so far unsuccessful at getting any to work. I'd be really grateful if there are any bright ideas about fixing this.