-1

I am having trouble with the simple task of passing a NSArray from the parent UIViewController down to a subview, UIView.

Here is some code...

I declare my subview (a subclass of UIView). Then in the view controller when the storyboard initialises, pass my array down to my subview. I log the array, and the console tells me (null).

ParentViewController.h

@property (nonatomic, strong) IBOutlet APColumnTableView *tableView;

ParentViewController.m

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
        [_tableView setColumnTitles:array];
        NSLog(@"%@", _tableView.columnTitles);
    }
    return self;
}

I have added the UIView in my Storyboard UIViewController, set the subclass and connected its IBOutlet. I have declared the columnTitles NSArray as you can see.

APColumnTableView.h (my subview subclass)

@property (nonatomic, strong) NSArray *columnTitles;

Then in the .m file, I log the array again to see what the console spits out, still (null).

APColumnTableView.m

-(void)awakeFromNib
{
    //Intialisation here...
    NSLog(@"%@", self.columnTitles);
}

Where am I going wrong?! I just want to simply pass an NSArray down from my UIViewController to its subview.

jscs
  • 63,694
  • 13
  • 151
  • 195
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • Thats great, because it doesn't help at all. I have looked this up without an answer. I am not passing data back, I am not going between view controllers which virtually all of these existing answers cover. Can you not look at the code I have posted and tell me what I am doing wrong? I wouldn't post without being unable to find an existing answer. – Josh Kahane Oct 12 '14 at 18:58

3 Answers3

2

None of your outlets are going to be set yet in initWithCoder:. Use awakeFromNib or viewDidLoad to pass the array. If you log _tableView in initWithCoder:, it's probably going to be nil.

Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46
  • Aaron's answer is correct. (Voted). In your init method your view controllers views are not loaded yet. viewDidLoad is the usual place to put code that deals with your views. – Duncan C Oct 12 '14 at 19:00
  • Ahh! The reason I haven't put it in `viewDidLoad` is because by the time the array is set from there, `awakeFromNib` in my subview has already been called. So how do I make sure I can set the array from the parent, then have the initialisation in the subview happen afterwards? – Josh Kahane Oct 12 '14 at 19:03
  • In your table view subclass, one way would be to override the setter and then just reload the table view when the data is set. Therefore you wouldn't have to rely on the order which methods are called at all. I'm not too familiar with `awakeFromNib` and the like since I don't use xib files or storyboards so perhaps someone else has a better answer. – Aaron Wojnowski Oct 12 '14 at 19:06
  • Scratch that thought, I've gotten around that issue by simply adding a method to my subview so that I may initialise UI and other elements such as the array at any point from my parent VC. Equally overriding the setter seems like a workable plan. Thanks. – Josh Kahane Oct 12 '14 at 19:06
0

You can not be guaranteed that _tableView already exists when initWithCoder is called for your ParentViewController. In other words, only in awakeFromNib can you be sure that all of your views are present and accounted for.

So move your code from initWithCoder to awakeFromNib within your ParentViewController and everything should be fine.

user965972
  • 2,489
  • 2
  • 23
  • 39
0

As I remember in initWithCoder your views are not created yet (so the _tableView is nil). Try doing it in viewDidLoad.

Rychu
  • 1,028
  • 1
  • 9
  • 20