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

I was looking for passing label data to another view controller and found the following solution. However, it does not display 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 the FirstViewController implementation

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

      

+3


source to share


2 answers


1. Save the contents of the lines in the first implementation file and load it in the second

Save your label.text in the first class:

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

      

Load it into the second class:

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

      



2. Use parameters

In the first class, you need to implement the second. #import "SecondClassName.h"


In the second class, create a function with parameters.

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

      

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

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

      

+1


source


Better to pass NSString

, then NSUserDefaults

. It's just better for data transfer if you useNSString

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.Stringdata = _label.text; // Set the exposed property
[self.navigationController pushViewController:secondViewController animated:YES];

      

In SecondViewController



@interface SecondViewController :UIViewController
{

}    
@property (nonatomic, retain) NSString* name;

      

In place of setting the text in the label, you can use

Label.text = Stringdata

      

+1


source







All Articles