在进入另一个视图控制器后计算uilabel的高度失败

时间:2012-10-30 04:43:35

标签: ios uilabel parameter-passing

所以我在将UIlabel文本高度传递给另一个窗口/ viewcontrollers

后计算它有问题

继承我传递的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NetraDealsObject *dataObject=[self.deals_data_json objectAtIndex:indexPath.row];

    detailViewController = [[MJDetailViewController alloc] initWithNibName:@"MJDetailViewController" bundle:nil];
    [self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];
    detailViewController.NetraPopupPrice.text=dataObject.formatted;

}

例如text == 123;

这里是detailviewController

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){

        NetraPopupPrice=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        NetraPopupPrice.backgroundColor=[UIColor colorWithRed:0.31 green:0.733 blue:0 alpha:1]; /*#4fbb00*/
        NetraPopupPrice.textColor=[UIColor whiteColor];
        NetraPopupPrice.textAlignment=NSTextAlignmentCenter;
        NetraPopupPrice.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
        [NetraPopupPrice.layer setCornerRadius:3];


        NetraPopupProvider=[[UILabel alloc] initWithFrame:CGRectMake(135.0f, 55, 80, 16)];
        NetraPopupProvider.backgroundColor=[UIColor clearColor];
        NetraPopupProvider.textColor=[UIColor colorWithRed:0.769 green:0.776 blue:0.788 alpha:1];
        NetraPopupProvider.font=[UIFont fontWithName:@"Helvetica" size:13];

        //separator
        NetraPopupSeparator=[[UILabel alloc] initWithFrame:CGRectMake(215,55.0f, 80, 16)];
        NetraPopupSeparator.text=@"-",
        NetraPopupSeparator.backgroundColor=[UIColor clearColor];
        NetraPopupSeparator.textColor=[UIColor colorWithRed:0.769 green:0.776 blue:0.788 alpha:1];
        NetraPopupSeparator.font=[UIFont fontWithName:@"Helvetica" size:13];

        //
        //separator
        NetraPopupLocation=[[UILabel alloc] initWithFrame:CGRectMake(225,  55.0f, 70, 16)];
        NetraPopupLocation.backgroundColor=[UIColor clearColor];
        NetraPopupLocation.textColor=[UIColor colorWithRed:0.769 green:0.776 blue:0.788 alpha:1];
        NetraPopupLocation.font=[UIFont fontWithName:@"Helvetica" size:13];

        NetraPopupHeadline=[[UILabel alloc] init];
        NetraPopupHeadline.textColor=[UIColor colorWithRed:0.227 green:0.243 blue:0.247 alpha:1];
        NetraPopupHeadline.lineBreakMode=NSLineBreakByCharWrapping;
        NetraPopupHeadline.backgroundColor=[UIColor redColor];
        NetraPopupHeadline.numberOfLines=0;
        NetraPopupHeadline.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:13];



    }
    [[self view] addSubview:NetraPopupPrice];

    [[self view] addSubview:NetraPopupProvider];
    [[self view] addSubview:NetraPopupSeparator];
    [[self view] addSubview:NetraPopupLocation];
    [[self view] addSubview:NetraPopupHeadline];
    return self;
}

-(void)viewDidLoad{
[super viewDidLoad];
    CGSize NetraLabelForPriceWidth = [[NetraPopupPrice text] sizeWithFont:[NetraPopupPrice font]];
    CGFloat NetraLabelForPriceW = NetraLabelForPriceWidth.width;

    NSLog(@"Width=%f",NetraLabelForPriceW);
}
-(void)viewWillAppear:(BOOL)animated{

}
- (void)viewDidUnload {

    [super viewDidUnload];
}

@end

和记录时的结果

NSLog(@"Width=%f",NetraLabelForPriceW);

是:

[9858:19d03] Width=0.000000

应该算一算吗?我的代码有什么不对吗?

2 个答案:

答案 0 :(得分:1)

尝试在viewDidLoad中记录 NetraPopupPrice及其文字的地址。还可以尝试在initWithNibName的if(self)块中添加“Subview”。

答案 1 :(得分:0)

在设置文本之前,您当前首先显示视图。因此,当viewDidLoad被调用时,[NetraPopupPrice text]仍为nil,从而导致NetraLabelForPriceW成为0

相反,你应该放置

detailViewController.NetraPopupPrice.text=dataObject.formatted;

[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];

以便在调用详细视图控制器的viewDidLoad时,[NetraPopupPrice text]已包含正确的字符串。