Logan Larroque Logan Larroque - 6 days ago 5
iOS Question

Passing label data from one view controller to the other

In my first view controller, a label shows the answer to the equation inputted by the user. When the user goes to the next view controller, I'd like the label in the second view controller to have the same data as received in the first one. I have read through a lot of passing data articles and questions on the internet (including here on SO). None seem to fall under my problem.

For example, I have 2 text fields and a label in my first view controller. If the user puts the number 2 in both fields, the label will show 4 as the answer. I'd then like the answer shown in the label to be available when the user goes on to the next view controller which explains the answer in depth.

EDIT: I'll put this in terms of my project so I can understand it better. I really appreciate all the responses!

In my first view controller named 'View Controller', the user calculates their BMI which shows up in a label known as 'bmiView'.

In my second view controller (TipsViewController), it shows the classification of the user. For example, if in the users calculated BMI from the first view controller is 30.00 they are classified as 'Obese' which I would like to show in the second view controller.

Answer

In FirstViewController suppose you have a UILabel *myLabel

myLabel.text =@"Hari kishan ";

SecondviewController and you have another label in second view controller.

@property(nonatomic, retain)NSString *secondLabel;
@property(nonatomic, retain)NSString *secondLabelText; //a string to contain text for that Label

//To set text of secondLabel as mylabel 
//do this in **FirstViewController.m**

SecondvViewController *objSecondviewController =[[SecondviewController alloc]init];
objSecondviewController.secondLabelText=myLabel.text;
[self.navigationController pushViewController:objSecondviewController animated:YES];

Now In SecondvViewController.m

self.secondLabel.text=secondLabelText;
Comments