我一直在寻找将标签数据传递到另一个视图控制器的方法,并找到了以下解决方案。但是,它不会在另一个视图控制器中显示标签文本。
请帮忙
SecondViewController标头
@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保存在第一堂课中:
[[NSUserDefaults standardUserDefaults] setObject:Label.text forKey:@"Your key"];
在第二类中加载它:
Label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Your key"];
2.使用参数
在第一类中,您必须实现#import "SecondClassName.h"
第二类。在第二类中,创建带有参数的函数。
- (void)setLabelText:(NSString *)LabelText
{
Label.text = LabelText
}
在第一堂课中,您通过添加以下两行来传递数据:
SecondViewControllerName *Second = [[SecondViewControllerName alloc] init];
[Second setLabelText:label.text];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句