如何在视图控制器 StoryBoard Xcode 之间发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14140679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Send data between view controllers StoryBoard Xcode
提问by user777304
Please help me..
请帮我..
I want to pass data between different controllers in storyboard. For Example.
我想在故事板中的不同控制器之间传递数据。例如。
First View Controller
第一个视图控制器
NSString *firstValue;
firstValue = @"First Number";
Now I want to send this to the result view controller and store in
现在我想把它发送到结果视图控制器并存储在
NSString *resultFromFirstVC;
and show in label.
并在标签中显示。
[label setText: resultFromFirstVC];
so the label show:
所以标签显示:
First Number
第一个数字
回答by Nikola Kirev
Use this method to pass the property in all of your view controllers
使用此方法在所有视图控制器中传递属性
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowNewVC"]) {
NextViewController *nextVC = (NextViewController *)[segue destinationViewController];
nextVC.someProperty = self.myProperty;
}
}
Change the name of "NextViewController" to match yours. Be sure to add the "Segue Identifiers" in your Storyboard file.
更改“NextViewController”的名称以匹配您的名称。请务必在 Storyboard 文件中添加“Segue Identifiers”。
This should work. However, when you have to pass this property trough so many view controllers, you can consider creating a Singleton "Data Manager" object to store the data. The View Controllers would have access to the data through the singleton.
这应该有效。但是,当您必须通过如此多的视图控制器传递此属性时,您可以考虑创建一个单例“数据管理器”对象来存储数据。视图控制器可以通过单例访问数据。
Good luck!
祝你好运!