4

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
}

2 Answers2

2

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.

joakim
  • 3,533
  • 2
  • 23
  • 28
0

Sorry, this doesn't work. You are creating your customCell via alloc/init in code. AwakeFromNib is only called for objects defined in a nib file.

You probably need to define your customCell in a separate nib file, and load that nib file through + (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner or similar.

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
iljawascoding
  • 1,090
  • 9
  • 20
  • I don't think I'm creating your customCell via alloc/init programmatically. The reason why I have "customCell *cellVC = [[cutsomCell alloc] init];" is because I want to do get the labels height. –  Feb 02 '15 at 23:02