从另一个ViewController获取项目的高度 [英] Get height of item from another ViewController

查看:54
本文介绍了从另一个ViewController获取项目的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView中有一个自定义单元格。我以编程方式创建了一个高度为200,宽度为50的标签。当我在标签的宽度和高度的customCell.m中执行NSLog时,它给了我w:50和h:200。但是当我做一个NSLog时在mainViewController.m中,它给出了0的高度和宽度。



不知道为什么会这样做。我需要在mainViewController.m中获得标签的真实高度



这是我的代码:



I have a custom cell in a UITableView. I programatically created a label with a height of 200 and a width of 50. When I do an NSLog in the customCell.m of the label's width and height, it gives me w: 50 and h: 200. But when I do an NSLog in mainViewController.m it gives me 0 for the height and width.

Not sure why it does that. I need to get the real height of the label in the mainViewController.m

Here's my code:

customCell.m

- (void)awakeFromNib {

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
    [self.label setBackgroundColor:[UIColor yellowColor]];
    [self.label setText:@"Hello"];
    [self.myView addSubview:self.label];

    CGRect myFrame = self.myView.frame;
    myFrame.size.height = self.label.frame.size.height;

    self.myView.frame = myFrame;
    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, cellVC.frame.size.height); // Results: 0.0000, 44.0000
}



我在storyBoard中将myView设置为0。


I set myView to 0 in the storyBoard.

推荐答案

在awakeNib的代码中设置标签的大小。在你的viewDidload中,它不会被新实例调用。



PS:customCell是一个糟糕的类名
in your code in the awakeNib you set the size of the label. In your viewDidload it wont get called by the new instance.

PS: customCell is a bad class name


这篇关于从另一个ViewController获取项目的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆