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;

}

たとえば、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

それは正しく計算する必要がありますか?私のコードに何か問題がありますか?

4

2 に答える 2

1

あなたのアドレスNetraPopupPriceとそのテキストをviewDidLoadに記録してみてください。また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];

詳細View ControllerviewDidLoadが呼び出されたときに、[NetraPopupPrice text]すでに正しい文字列が含まれているようにします。

于 2012-10-30T05:15:02.770 に答える