如何将标签数据传递给第二个视图控制器中的另一个标签

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

我一直在寻找将标签数据传递给另一个视图控制器的方法,并找到了以下解决方案。但是,它不会在另一个视图控制器中显示标签文本。

请帮忙

SecondViewControllerHeader

@interface SecondViewController : UIViewController 
{
    IBOutlet UILabel *copy;
}

@property (nonatomic, retain) NSString *data;

在 SecondViewController 实现中

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];

在 FirstViewController 实现中

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

1.将字符串内容保存在第一个实现文件中,在第二个

中加载

先保存你的 label.text class:

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

第二次加载class:

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

2。使用参数

第一个 class 你必须实现第二个 #import "SecondClassName.h"
在第二个 class 中创建一个带参数的函数。

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

在第一个 class 中,您通过添加以下两行来传递数据:

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

最好通过NSString然后NSUserDefaults。只是为了传递数据,如果你使用 NSString

会更好
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 the place to set text in label you can use

Label.text = Stringdata