0

Today I use MVC to build a photo album.But I can't get data in ViewControllerB that pass from ViewControllerA,I read this article here is link (Passing Data between View Controllers), and check all the steps I do.

Here is code in 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
}];

Here is code in ViewControllerB:

@interface PhotoViewController : UIViewController

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

@end

here is .m file

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

But I got nothing, I don't know where I was wrong, how to make it right?? Thanks a lot.

Community
  • 1
  • 1
chile
  • 141
  • 1
  • 12
  • Please check this [link][1] its clearly explained here. This is may help you to do better and understand better. [1]: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Bangalore Dec 26 '15 at 08:01
  • Yeah, I read this article for times some days ago, and I read it today for answers, I did step by step, but it doesn't work. – chile Dec 26 '15 at 08:20
  • have you tried using protocols? – Bangalore Dec 26 '15 at 08:22
  • Have you checked that `images` is actually non-nil before the call? Are you sure your view controller is instantiated through this code (and not via a storyboard segue for instance)? – jcaron Dec 26 '15 at 09:01
  • Thanks, I found the reason , I wrote some sentence in VCB's init method, when I moved them to viewDidLod method, data can be get. I don't try protocols, I use protocols send data from VCB to VCA. and that 'images' is not non-nil , storyboard segue I will learn it. – chile Dec 26 '15 at 12:24

2 Answers2

1

I don't think you can instantiate your PhotoViewController from:

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

Do this instead:

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

Or you should pass the identifier to nibName, otherwise how can it find out which Nib file to load?

EDIT: You should put this code section inside the animation completion block. UIViewController is not full instantiated when you pass data like you did.

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

EDIT 2: Reason:

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.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • I did init photoVC like `PhotoViewController *photoVC = [[PhotoViewController alloc] init];` but it is still can't pass data. – chile Dec 26 '15 at 08:41
  • If you surely get non-nil `photoVC`, see my edits. @chile – Lucas Huang Dec 26 '15 at 08:44
  • Is that `- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil` init method in PhotoViewController.m can not write anything ? – chile Dec 26 '15 at 08:47
  • You can put this designated initializer in your `PhotoViewController` with your custom parameters. Then, when you create it. You will need to use this initializer. – Lucas Huang Dec 26 '15 at 08:48
  • OK, it work, I did override the viewDidLoad, but i write some set like `self.view.backgroundColor = [UIColor lightGrayColor];` in `- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil` I write them in viewDidLoad method , and it worked. Thanks a lot. – chile Dec 26 '15 at 08:57
0

Storyboard equal between two controller and controller runing.

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
}];
Moosa
  • 89
  • 1
  • 8