0

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
docBen
  • 39
  • 7
  • 1
    `[Patient.weight] = weightTextFieldInterrogation;` is a syntax error. Is this the code you're actually using? – jscs Jun 19 '14 at 18:43
  • 1
    This question has nothing to do with objective-c and everything to do with OOP. Read some literature. http://www.raywenderlich.com/45940/intro-object-oriented-design-part-1 – CrimsonChris Jun 19 '14 at 18:46

1 Answers1

0

Your problem is that the object you create is destroyed almost immediately after you create it. You are expecting that the instance will float around so that you can pick it up again in the future but it won't.

There are some ways to cheat, but really you should be taking that instance and passing it to some other view controller which wants / needs it, or storing that float value somewhere else (like in a file on disk, or in user defaults).

This is really an OOP question about what you use objects for and how long they exist for.

Wain
  • 118,658
  • 15
  • 128
  • 151