UILabels将动态信息添加到标签中并单独列出每个标签?

时间:2014-03-07 15:11:38

标签: ios objective-c uilabel

尝试向我的视图控制器添加标签,但每次都会生成一个新标签,而不是将它们放在彼此之上。我试图改变代码,每次运行一个类时调用它,但它列出了我写的最后一个而不是全部。下面是视图controller.m

中的代码
//
//  ViewController.m
//  AOC2Project1
//
//  Created by Brian Stacks on 3/4/14.
//  Copyright (c) 2014 Brian Stacks. All rights reserved.
//

#import "ViewController.h"
#import "baseCar.h"
#import "carFactory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    mycarFactory =[[carFactory alloc]init];
    if (mycarFactory != nil)
    {
        //called class into action
    }

    myBaseCar = [[baseCar alloc]init];
    if (myBaseCar != nil) {
        //called class into action

    }

    myChevy = [[chevyCar alloc]init];
    if (myChevy != nil) {
        //called class into action

    }
    myFord = [[fordCar alloc]init];
    if (myFord != nil) {
        //called class into action

    }
    myChrysler = [[chryslerCar alloc]init];
    if (myChrysler != nil) {
        //called class into action
    }
    myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 275, 100)];
        myTextLabel.text = [myBaseCar myText];
    myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 275, 100)];
        myTextLabel.text = [myChevy myText];
    myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, 275, 100)];
        myTextLabel.text = [myFord myText];
    [self.view addSubview:myTextLabel];

    chevyCar *firstCar = (chevyCar*)[carFactory createNewCar:CreateChevroletType_Chevrolet];
    [firstCar setTimePerQuarterMile:5];
    NSLog(@" This where we are at %@",firstCar.carModel);


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

继承人.h文件

//
//  ViewController.h
//  AOC2Project1
//
//  Created by Brian Stacks on 3/4/14.
//  Copyright (c) 2014 Brian Stacks. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "carFactory.h"

@interface ViewController : UIViewController
{
    NSString *text;
    UILabel *myTextLabel;
    carFactory *mycarFactory;
    baseCar *myBaseCar;
    chevyCar * myChevy;
    fordCar * myFord;
    chryslerCar * myChrysler;
}

@end

1 个答案:

答案 0 :(得分:2)

您正在创建标签,但未将其添加到子视图

myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 275, 100)];
    myTextLabel.text = [myBaseCar myText];
myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 275, 100)];
    myTextLabel.text = [myChevy myText];
myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, 275, 100)];
    myTextLabel.text = [myFord myText];
[self.view addSubview:myTextLabel];

应该是这个

myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 275, 100)];
    myTextLabel.text = [myBaseCar myText];
[self.view addSubview:myTextLabel];
myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 275, 100)];
    myTextLabel.text = [myChevy myText];
[self.view addSubview:myTextLabel];
myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, 275, 100)];
    myTextLabel.text = [myFord myText];
[self.view addSubview:myTextLabel];

此外,您的[super ViewDidLoad];应位于方法的顶部