katie katie - 8 days ago 6
Objective-C Question

How to pass label data to another label in the second view controller

I've been searching for passing label data to another view controller, and found the following solution. However, it doesn't show the label text in another view controller.

please help

SecondViewController Header

@interface SecondViewController : UIViewController {


IBOutlet UILabel *copy;
}

@property (nonatomic, retain) NSString *data;


In SecondViewController Implementation

copy.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleBottomMargin;
copy.textAlignment = NSTextAlignmentCenter;
copy.text = [NSString stringWithFormat:@"Your data: %@", _data]; // Make use of the exposed data property
[self.view addSubview:copy];


In FirstViewController Implementation

- (void)passDataForward
{
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.data = _label.text; // Set the exposed property
[self.navigationController pushViewController:secondViewController animated:YES];
}

Answer

1. Save the strings content in the first implementation file and load it in the second

Save your label.text in first class:

[[NSUserDefaults standardUserDefaults] setObject:Label.text forKey:@"Your key"];

Load it in the second class:

Label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Your key"];

2. Use parameters

In the first class you have to implement the second #import "SecondClassName.h"
In second class create a function with parameters.

- (void)setLabelText:(NSString *)LabelText
{
    Label.text = LabelText
}

And in the first class you pass the data by adding these two lines:

SecondViewControllerName *Second = [[SecondViewControllerName alloc] init];
[Second setLabelText:label.text];
Comments