iphone - 使用标签/UILabels/UIViews

标签 iphone ios uiview tags uilabel

目前,

我正在我的应用程序中实现一个功能,该功能在 UIView 的 subview 中动态分配 UIViews 和 2 个 UILabels...我以前做过这个,但由于某种原因似乎停留在这个问题上。

这是我的代码:

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


}

这不应该有任何问题,因为当我创建一个新 View 时,我创建了一个新的 UILabel,并将其添加到新的 UIView 中......

这是一张照片:

simulator Pic

所以我创建了三个 View ,很好...但是我的 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

真的不确定为什么这不能正常工作...有人有任何想法吗?在这一点上,这似乎是一件非常微不足道的事情,就像一行代码之类的......我在使用具有多个 View 的标签之前已经这样做了,并且对于不同的 UIViews 具有不同的值,并且 UILabels 值随关于我实现的 UISlider。

所以我对这个问题有一些经验,但我现在很困惑。也许明天我会明白我做错了什么,但目前……我真的不确定……如果有人可以指点……请这样做:)

最佳答案

您错误地设置了标签框架:

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

Frame 已经相对于父 View ,因此当您添加父 View 的原点 xy 时,您的标签会超出可见范围。这对于第一个 View 工作正常,因为它的 xy 很小。

你可能想写:

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

第二个标签也一样。

关于iphone - 使用标签/UILabels/UIViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689578/

相关文章:

iphone - 如何替换已弃用的方法 dispatch_get_current_queue()?

ios - 如何在 Swift 中从 SKScene 呈现 UiView

objective-c - UIView 改变方向并出现丑陋的角落

iphone - TouchDown、TouchUpInside、TouchUpOutside、TouchCancel 事件的顺序是什么?

iphone - 使用selector方法调用C Function并在iPhone的c方法中访问self

iphone - 如何管理用户单击徽章时的通知

ios - 在 UIGestureRecognizerState 不工作的情况下点击手势

iphone - 我何时以及为何需要释放对象?

ios - 重用 UITableViewHeaderFooterView 时出现 NSAutoresizingMaskLayoutConstraint 错误

ios - addSubView - 在 UIView 中添加一个按钮也会为其 SuperView 添加相同的按钮