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

标签 ios objective-c

今天我使用 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/

相关文章:

iphone - 检测网络是否发生变化?

ios - presentViewController 重置所有 UIView 动画

ios - [object] 没有可见的@interface 声明选择器 [method]

ios - TableView - 在部分中自定义标题

ios - kCLLocationAccuracyBestForNavigation 与 kCLLocationAccuracyBest

ios - 是否有分配 UITabBarController 管理的 Controller 属性的最佳实践?

ios - 如何用弹出窗口样式显示 UISplitViewController 的主视图?

iPhone游戏编程: Multiple UIImageViews in a UIViewController or UIImages in a UIView?

ios - 在 iOS 上从 Yahoo 获取 OAuth session 句柄

ios - 为 iOS 创建从右到左的用户界面