0

所以我在将它传递给另一个窗口/视图控制器后计算 UIlabel 文本高度时遇到问题

这是我要传递的代码

- (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;

}

例如文本==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

它应该是正确的计算?我的代码有什么问题吗?

4

2 回答 2

1

尝试在 viewDidLoad 中记录您的地址NetraPopupPrice及其文本。if(self)还可以尝试在 initWithNibName 块内“添加到子视图” 。

于 2012-10-30T05:35:52.143 回答
0

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

相反,您应该放置

detailViewController.NetraPopupPrice.text=dataObject.formatted;

[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];

这样当您的详细视图控制器viewDidLoad被调用时,[NetraPopupPrice text]就已经包含正确的字符串了。

于 2012-10-30T05:15:02.770 回答