gpt4 book ai didi

ios - MVC 将数据从一个 ViewController 传递到另一个 ViewController 失败(所有步骤均已完成)

转载 作者:行者123 更新时间:2023-11-29 01:22:12 25 4
gpt4 key购买 nike

今天我使用 MVC 构建相册。但是我无法在 ViewControllerB 中获取从 ViewControllerA 传递的数据,我在这里阅读这篇文章是链接(Passing Data between View Controllers),并检查我所做的所有步骤。

这是 ViewControllerA 中的代码:

PhotoViewController *photoVC = [[PhotoViewController alloc] initWithNibName:nil bundle:nil];
photoVC.images = images;
photoVC.index = index;
photoVC.view.backgroundColor = [UIColor lightGrayColor];
[self presentViewController:photoVC animated:YES completion:^{
// code
}];

这是 ViewControllerB 中的代码:

@interface PhotoViewController : UIViewController

@property (nonatomic ,retain) NSMutableArray *images;
@property (nonatomic, assign) NSInteger index;

@end

这里是.m文件

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@", self.images);
}

但是我一无所获,我不知道我哪里错了,如何改正??非常感谢。

最佳答案

我不认为你可以实例化你的 PhotoViewController 来自:

PhotoViewController *photoVC = [[PhotoViewController alloc] initWithNibName:nil bundle:nil];

改为这样做:

PhotoViewController *photoVC = [[PhotoViewController alloc] init];

或者您应该将标识符传递给nibName,否则它如何找出要加载哪个 Nib 文件?

编辑:您应该将此代码部分放在动画完成 block 中。 UIViewController 在您传递数据时未完全实例化。

[self presentViewController:photoVC animated:YES completion:^{
photoVC.images = images;
photoVC.index = index;
photoVC.view.backgroundColor = [UIColor lightGrayColor];
}];

编辑 2:原因:

The nib file you specify is not loaded right away. It is loaded the first time the view controller's view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.

关于ios - MVC 将数据从一个 ViewController 传递到另一个 ViewController 失败(所有步骤均已完成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469528/

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