gpt4 book ai didi

ios - 如何将数据传递给使用 instantiateViewControllerWithIdentifier 创建的 ViewController?

转载 作者:行者123 更新时间:2023-11-29 13:09:02 24 4
gpt4 key购买 nike

我创建了一个使用大量容器 View 的应用程序。今天我发现 iOS 5 不支持嵌入式 segues(感谢 Xcode 让我知道。)所以现在我有很多嵌入式 segue,它们可以在彼此之间传递数据。

我正在使用此代码在 UIView 中加载 ViewController,而不是从 IB 加载容器:

Root View Controller :

- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
news.view.frame = self.newsSection.bounds;
[self.newsSection addSubview:news.view];
[self addChildViewController:news];
[news didMoveToParentViewController:self];
// Do any additional setup after loading the view.
}

我怎样才能使用上面的方法并仍然将数据传递给 NewsViewController(类似于为 segue 做准备)

我试过在上面插入类似的东西

        NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
news.view.frame = self.newsSection.bounds;
news.myLabel.text = @"Test";
[self.newsSection addSubview:news.view];

但标签不会改变。

我更喜欢只传递数据而不是使用单例或委托(delegate)。

谢谢

最佳答案

我建议创建一个公共(public)方法,您可以从中将所需的数据传递到您的 NewsViewController Controller 上。尽管如此,现在的问题是您在 UI 初始化之前更新它。此外,我建议您查看如何创建内容 UIViewControllers,因为现在您只是添加其 UIView。检查this来自 Apple 的文档,请查看实现自定义容器 View Controller 部分。这部分代码:

- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self]; // 3
}

因此,在您的情况下,完成第 3 步后,您可以传递要在 NewsViewController 中使用的值。

关于ios - 如何将数据传递给使用 instantiateViewControllerWithIdentifier 创建的 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17855330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com