objective-cuitableviewxibcustom-cellawakefromnib

How to Call awakeFromNib in mainViewController.m


I have an xib file containing a custom cell. I'm trying to access the height of an object created in customCell.m.

Here is my code:

customCell.m

- (void)awakeFromNib
{
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
    [self.label setText:@"This is a label"];
    [self.myView addSubview:self.label];

    NSLog(@"%f", self.label.frame.size.height);  // Results: 200.0000
}

mainViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    customCell *cellVC = [[cutsomCell alloc] init];

    NSLog(@"%f, %f", cellVC.label.frame.size.height); // Results: 0.0000
}

Solution

  • awakeFromNib is called when all file owners outlets and properties are set. Things are not wired up (in terms of frames/layout) in viewDidLoad.

    awakeFromNib is called after viewDidLoad, that is why you see the difference.