问题描述
请帮帮我..
我想在故事板中的不同控制器之间传递数据.例如.
I want to pass data between different controllers in storyboard.For Example.
第一个视图控制器
NSString *firstValue;
firstValue = @"First Number";
现在我想把它发送到结果视图控制器并存储在
Now I want to send this to the result view controller and store in
NSString *resultFromFirstVC;
并显示在标签中.
[label setText: resultFromFirstVC];
所以标签显示:
第一个数字
推荐答案
使用此方法在所有视图控制器中传递属性
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;
}
}
更改NextViewController"的名称以匹配您的名称.请务必在 Storyboard 文件中添加Segue Identifiers".
Change the name of "NextViewController" to match yours.Be sure to add the "Segue Identifiers" in your Storyboard file.
这应该有效.但是,当您必须通过如此多的视图控制器传递此属性时,您可以考虑创建一个单例数据管理器"对象来存储数据.视图控制器可以通过单例访问数据.
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.
祝你好运!
这篇关于如何在视图控制器 StoryBoard Xcode 之间发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!