How to call awakeFromNib in mainViewController.m

I have an xib file containing a custom cell. I am 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
}
+4
source share
2 answers

awakeFromNib is called when all feeds and file properties are set. Things are not connected (in terms of frames / layouts) in viewDidLoad.

awakeFromNib is called after viewDidLoad, so you see the difference.

+2
source

Sorry, this does not work. You create your customCell via alloc / init in code. AwakeFromNib is called only for objects defined in the nib file.

, customCell nib nib + (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner .

0

All Articles