简体   繁体   English

使用Tags / UILabels / UIViews

[英]Working with Tags/UILabels/UIViews

Currently, 目前,

I am implementing a feature in my app that dynamically allocate UIViews with 2 UILabels within the UIView's subview... I have done this before but seem to be stuck on this for some reason. 我正在我的应用程序中实现一个功能,在UIView的子视图中动态分配UIViews和2个UILabel ...之前我已经完成了这个但是由于某些原因似乎被困在这个上面。

Here's my code: 这是我的代码:

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);
}

I have also tried : 我也尝试过:

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];


}

Which shouldn't have any issues because when I create a new view, I create a new UILabel that I add to the NEW UIView... 这不应该有任何问题,因为当我创建一个新视图时,我创建了一个新的UILabel,我添加到NEW UIView ...

Heres a pic: 下图是:

模拟器Pic

So I create three views, okay good... But where are my 3 UILabels.... Also heres a log statement of the texts: 所以我创建了三个视图,好的...但是我的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

Really not sure why this isnt working correctly... Anyone got any ideas? 真的不确定为什么这不能正常工作......任何人都有任何想法? This seems like a very trivial thing at this point, like one line of code or something.... I have done this before using tags with multiple views like this and have different values with respect to different UIViews, and the UILabels values change with respect to a UISlider I implemented. 这似乎是一个非常微不足道的事情,就像一行代码或者其他东西......我在使用像这样的多个视图的标签之前做了这个,并且对于不同的UIViews有不同的值,并且UILabels值随着尊重我实施的UISlider。

So I have some experience with this issue but I am stump at the moment. 所以我对这个问题有一些经验,但我现在很难过。 Maybe tomorrow I'll understand what I'm doing wrong but at the moment.... I'm really not sure... If anyone can give a pointer... please do so :) 也许明天我会理解我做错了什么,但此刻......我真的不确定......如果有人能给出指针......请这样做:)

You're setting your labels frame incorrectly: 您错误地设置了标签框:

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

Frame is already relative to parent view, so when you add parent's origin x and y , your label gets out of visible bounds. 帧已经相对于父视图,因此当您添加父的原点xy ,您的标签将超出可见边界。 This works ok for the first view, because its x and y are small. 这适用于第一个视图,因为它的xy很小。

You might want to write: 你可能想写:

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

The same for the second label. 第二个标签也一样。

label.textColor = [UIColor whiteColor];

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

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

when the statement of **powerOutput.textAlignment = NSTextAlignmentCenter;** is first running, your variable **powerOutput** is **nil** . **powerOutput.textAlignment = NSTextAlignmentCenter;**的语句首次运行时,您的变量**powerOutput****nil**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM