Ios 计算uilabel的高度传递到ViewController后失败

Ios 计算uilabel的高度传递到ViewController后失败,ios,uilabel,parameter-passing,Ios,Uilabel,Parameter Passing,因此,在将UIlabel文本高度传递给另一个窗口/视图控制器后,我无法计算它 这是我的密码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NetraDealsObject *dataObject=[self.deals_data_json objectAtIndex:indexPath.row]; detailViewController

因此,在将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);
是:


应该是吧?我的代码有什么问题吗?

在设置文本之前,您当前正在首先显示视图。因此,当调用
viewDidLoad
时,
[netrapopupplice text]
仍然是
nil
,导致您的
NetraLabelForPriceW
变为
0

相反,你应该

detailViewController.NetraPopupPrice.text=dataObject.formatted;
以前

[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];

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

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

仍然返回[10418:19d03]Width=0.000000我已经将代码放在您的建议中—(void)viewDidLoad{[super viewDidLoad];NSLog(@“NetraText%@”,netrapopupplice.text);}[10811:19d03]NetraText(null)为什么它返回null?但在应用程序中显示如何添加子视图?这就像[[self-view]addSubview:NetraPopupProvider];是的,在if(self)块内执行此操作。在if块内添加子视图,而不是在其外部。(不确定,但你可以试试)。我认为分析你的代码或使用它的副本进行调整可能会有所帮助。有时使用一些NSLog(就像我在回答中提到的那样)让你朝着正确的方向前进。有助于学习。
[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];