Logan Larroque
Logan Larroque

Reputation: 235

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.

Upvotes: 0

Views: 3892

Answers (7)

h.kishan
h.kishan

Reputation: 681

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;

Upvotes: 1

Bhavin
Bhavin

Reputation: 27225

Steps :

1) Create Property called final in Next View Controller :

@property(nonatomic, strong) NSString *final;

2) Calculate the Sum :

int firstVal = [yourFirstTextField.text intValue];
int secondVal = [yourSecondTextField.text intValue];
int resultVal = firstVal + secondVal;
NSString *final = [NSString stringWithFormat:@"%d",resultVal];

3) Send it to Next View Controller :

NextViewController *navController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
nextViewObj.final = final;
[self.navigationController pushViewController:navController animated:YES];

4) In Next View Controller :

yourLabel.text = self.final;

Upvotes: 0

Lithu T.V
Lithu T.V

Reputation: 20021

in view controller 2 make a property of string

@property (nonatomic, strong) NSString * resultValue;

in viewdidLoad

[self.textfieldOutlet setText:self.resultValue];

When initializing 2nd View controller

Viewcontroller2 *val=[Viewcontroller2 alloc ]init];
val.resultValue = self.resulttextFieldoutletinVC1.text;

push/modal the viewcontroller

So just pass the value in a string and you have the value in the 2nd view controller and do whatever logic on it

Upvotes: 0

iphondroid
iphondroid

Reputation: 500

Create an NSString named labelText in your SecondViewController.h and let the name of your label in FirstViewController be myLabel then do this in your 'FirstViewController' :

SecondViewController *view = [[SecondViewController alloc] init];
view.labelText = myLabel.text;

Upvotes: 0

stosha
stosha

Reputation: 2148

You can use NSUserDefaults. In first view controller if your label value is changed you can save value:

[[NSUserDefaults standardUserDefaults] setObject: label.text
                                          forKey: @"lableValue"];

in second view controller:

- (void) viewDidAppear: (BOOL) animated
{
     label.text = [[NSUserDefaults standardUserDefaults] stringForKey: @"lableValue"];
}

Upvotes: 0

Kapil Choubisa
Kapil Choubisa

Reputation: 5232

You can create property of string in secondView

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController {
}
@property (nonatomic, strong) NSString *strResultedText;
@end

than you can pass your label's value to second view from first before pushing or presenting. You can access the property using the SecondView's object like:

SecondViewController *obj = //your initialization
obj.strResultedText = lbl.text;

Upvotes: 0

iphonic
iphonic

Reputation: 12719

FirstController

-(void)calculateAnswer{
    int answer=[self.firstInput.text intValue] + self.secondInput.text intValue];

    SecondController *controller=[[SecondController alloc] initWithNibName:@"SecondView" bundle:nil];
    //Answer is a PUBLIC property declared in SecondController
    controller.answer=[NSString stringWithFormat:@"%d",answer];
    [self.navigationController pushViewController:controller animated:YES];
}

SecondController

In SecondController.h

@property(nonatomic, retain)NSString *answer;

In SecondController.m

-(void)viewDidLoad{
  [super viewDidLoad];

  self.lblAnswer.text=self.answer;
}

Upvotes: 0

Related Questions