0

I've read a bunch of tutorials were and on the web and I've watched countless videos and I still have no clue how to use (pass?) data from different view controllers.

In my project, the user inputs their height and weight into text fields and a label shows their BMI. In my second view controller (TipsViewController) I'd like to use the BMI from the first view controller (ViewController) to have the proper information present to the user. For example:

If the user comes up with a BMI of 32.00, the label in ViewController will show the BMI of 32.00. In TipsViewController, since the user has a BMI of 32.00, the view will show information for that of an 'Obese' person.

I have no clue how to use the data from the first view controller to my second one. I have followed tutorials on AppDelegates and Singletons but I have no idea how to use them in my application. Any help would be greatly appreciated.

My BMI label is 'bmiView'.

NOTE: I'm using storyboards if that helps at all.

Logan Larroque
  • 235
  • 1
  • 3
  • 7
  • http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ – stosha Jul 27 '13 at 01:29

1 Answers1

1

Add a property on the TipsViewController, something like:

@property float bodyMassIndex;

In the ViewController (first view controller) implement the prepareForSegue method, and set the value of the BMI to the TipsViewController, e.g.:

myTipsViewController.bodyMassIndex = calculatedBodyMassIndex;

Then the TipsViewController can access the BMI with self.bodyMassIndex

bobnoble
  • 5,794
  • 3
  • 25
  • 32
  • so in the first view controller: `TipsViewController.bodyMassIndex = bmiView.text;`? Would 'calculatedBodyMassIndex' be my label from the first view controller? – Logan Larroque Jul 27 '13 at 01:21