使用Tags / UILabels / UIViews

时间:2013-07-17 01:22:39

标签: iphone ios uiview tags uilabel

目前,

我正在我的应用程序中实现一个功能,在UIView的子视图中动态分配带有2个UILabel的UIViews ...我之前已经这样做了,但似乎因某种原因而被困在这个上。

这是我的代码:

UIView *view;
UILabel *label;
UILabel *powerOutput;

for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++)
{
    view = [[UIView alloc] initWithFrame:CGRectMake((i-1)*100 + 5, 10, 90, 100)];
    view.backgroundColor = [UIColor blackColor];

    label = [[UILabel alloc] init];
    label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:10];
    label.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
    [powerOutput setTag:LABEL_TAG+i];
    NSLog(@"%@", label.text);
    [view addSubview:label];

    powerOutput = [[UILabel alloc] init];
    powerOutput.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 50, view.frame.size.width - 10, 20);
    powerOutput.backgroundColor = [UIColor clearColor];
    powerOutput.font = [UIFont systemFontOfSize:10];
    powerOutput.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    powerOutput.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
    [powerOutput setTag:IMAGE_TAG+i];
    NSLog(@"%@", powerOutput.text);
    [view addSubview:powerOutput];

    [self.view addSubview:view];


}

for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++) {

    UILabel *label = (UILabel *)[self.view viewWithTag:LABEL_TAG+i]; // get the label with tag
    //NSLog(@"%@", label);
    label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
    //NSLog(@"%@", label.text);

    UILabel *powerOutputLabel = (UILabel *)[self.view viewWithTag:IMAGE_TAG+i]; // get the label with tag
    powerOutputLabel.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
    //NSLog(@"%@", powerOutputLabel.text);
}

我也尝试过:

UIView *view;
UILabel *label;
UILabel *powerOutput;

for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++)
{
    view = [[UIView alloc] initWithFrame:CGRectMake((i-1)*100 + 5, 10, 90, 100)];
    view.backgroundColor = [UIColor blackColor];

    label = [[UILabel alloc] init];
    label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:10];
    label.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
    //[powerOutput setTag:LABEL_TAG+i];
    NSLog(@"%@", label.text);
    [view addSubview:label];

    powerOutput = [[UILabel alloc] init];
    powerOutput.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 50, view.frame.size.width - 10, 20);
    powerOutput.backgroundColor = [UIColor clearColor];
    powerOutput.font = [UIFont systemFontOfSize:10];
    powerOutput.textColor = [UIColor whiteColor];
    powerOutput.textAlignment = NSTextAlignmentCenter;
    powerOutput.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
    //[powerOutput setTag:IMAGE_TAG+i];
    NSLog(@"%@", powerOutput.text);
    [view addSubview:powerOutput];

    [self.view addSubview:view];


}

哪个应该没有任何问题,因为当我创建一个新视图时,我创建了一个新的UILabel,我添加到了新的UIView ...

下图:

simulator Pic

所以我创建了三个视图,好的...但是我的3个UILabel在哪里....还有一个日志声明:

2013-07-16 18:14:26.945 PowerOneApp[14307:c07] SN 875222
2013-07-16 18:14:26.945 PowerOneApp[14307:c07] 53.50W
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] SN 875225
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] 59.89W
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] SN 957188
2013-07-16 18:14:26.947 PowerOneApp[14307:c07] 135.39W

真的不确定为什么这不能正常工作......任何人都有任何想法?这似乎是一个非常微不足道的事情,就像一行代码或者其他东西......我在使用像这样的多个视图的标签之前做了这个,并且对于不同的UIViews有不同的值,并且UILabels值随着尊重我实施的UISlider。

所以我对这个问题有一些经验,但我现在很难过。也许明天我会理解我做错了什么,但此刻......我真的不确定......如果有人能给出指针......请这样做:)

2 个答案:

答案 0 :(得分:2)

您的标签框设置不正确:

label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);

框架已经相对于父视图,因此当您添加父项的原点xy时,您的标签会超出可见范围。这适用于第一个视图,因为其xy很小。

你可能想写:

label.frame = CGRectMake(10, 12, view.frame.size.width - 10, 20);

第二个标签也一样。

答案 1 :(得分:0)

label.textColor = [UIColor whiteColor];

***powerOutput.textAlignment = NSTextAlignmentCenter;***

label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];

**powerOutput.textAlignment = NSTextAlignmentCenter;**的语句第一次正在运行时,您的变量**powerOutput****nil**