Search code examples
ioscocoa-touchuiviewuiviewcontrolleriboutlet

Passing an NSArray from a UIViewController to a subview


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.


Solution

  • 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.