简体   繁体   English

在两个UIViewController之间传递数据

[英]Pass Data Between Two UIViewControllers

I need to pass data of UILabel from ViewB to ViewA. 我需要将UILabel的数据从ViewB传递到ViewA。 My ViewA has a UILabel with some number. 我的ViewA有一个带有一些数字的UILabel。 This number can be changed in ViewB which I open as a new UIViewController as below: 可以在ViewB中更改此数字,将其作为新的UIViewController打开,如下所示:

viewB = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];

ViewB also has a UILabel to hold the same value. ViewB还具有一个UILabel来保存相同的值。 I tried to pass this value from ViewB to ViewA by assigning UILabel's like so: 我试图通过分配UILabel的方法将此值从ViewB传递到ViewA,如下所示:

viewB.countdownLabel = self.countdownLabel;

That didn't work. 那没用。 Thanks for suggestions... 感谢您的建议...

我想你想要的是: viewB.countdownLabel.text = self.countdownLabel.text

确保viewB.countdownLabel是保留属性,并且应该可以使用。

By saying "viewB.countdownLabel = self.countdownLabel;" 通过说“ viewB.countdownLabel = self.countdownLabel;” you are simply making viewB's countdownLabel a reference to the original label, you're not copying their values. 您只是将viewB的countdownLabel用作对原始标签的引用,而不是复制其值。 You need to do the following... 您需要执行以下操作...

[viewB.countdownLabel setText:self.countdownLabel];

I would suggest that you use AppDelegate to pass the value across to any viewController (and read the documentation here ). 我建议您使用AppDelegate将值传递给任何viewController (并在此处阅读文档)。

GeneralAppDelegate*appDelegate=[[UIApplication sharedApplicaton] delegate];
myLocalProperty=appDelegate.someDataModelProperty;

or just use 或只是使用

viewB.countdownLabel.text = self.countdownLabel.text;

in your code. 在您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM