Iphone 使用标记/ui标签/ui视图

Iphone 使用标记/ui标签/ui视图,iphone,ios,uiview,tags,uilabel,Iphone,Ios,Uiview,Tags,Uilabel,目前, 我正在我的应用程序中实现一个功能,在UIView的子视图中动态分配带有2个UILabel的UIView。。。我以前做过这件事,但出于某种原因,我似乎被困在这件事上了 这是我的密码: UIView *view; UILabel *label; UILabel *powerOutput; for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++) { view = [[UIView alloc] init

目前,

我正在我的应用程序中实现一个功能,在UIView的子视图中动态分配带有2个UILabel的UIView。。。我以前做过这件事,但出于某种原因,我似乎被困在这件事上了

这是我的密码:

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具有不同的值,UILabels值对于我实现的UISlider也会发生变化


所以我在这个问题上有一些经验,但目前我还很难办。也许明天我会明白我做错了什么,但现在。。。。我真的不确定。。。如果有人能给出一个指针。。。请执行以下操作:)

您的标签框设置不正确:

label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
框架已经相对于父视图,因此当添加父视图的原点
x
y
时,标签将超出可见范围。这在第一个视图中正常工作,因为它的
x
y
都很小

您可能想写:

label.frame = CGRectMake(10, 12, view.frame.size.width - 10, 20);
第二个标签也是如此

label.textColor = [UIColor whiteColor];

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

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

**powerOutput.textAlignment=NSTextAlignmentCenter的语句**
首先运行,您的变量
**powerOutput**
**nil**

同意,或者在父视图中添加布局约束更好。嗯。。。可以明天我到办公室时,我要试一试。。感谢您的输入:)
label.frame = CGRectMake(10, 12, view.frame.size.width - 10, 20);
label.textColor = [UIColor whiteColor];

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

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