0

I have created dynamic UILabel with tag value and i have similar kind of other labels without tag values.

Now i want access only tag values labels but i was not able to acces the label text value.

Here is my code.

 Dynamic created Label
     for (int i= 0; i < [array count]; i++) {
        UILabel *defaultLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 60, 21)];
        defaultLbl.text = @"Default";
        defaultLbl.backgroundColor = [UIColor clearColor];
        defaultLbl.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
        [defaultLbl setFont:[UIFont fontWithName:@"Helvetica" size:12]];
        defaultLbl.textAlignment = NSTextAlignmentCenter;
        defaultLbl.tag = i+1;
        [myButton addSubview:defaultLbl];
        [defaultLbl release];

        UILabel *masterProName = [[UILabel alloc] initWithFrame:CGRectMake(28, 20, 200, 21)];
        masterProName.text = [masterProjListArray objectAtIndex:i];//@"Lorem Ipusum";
        masterProName.backgroundColor = [UIColor clearColor];
        masterProName.textColor = [UIColor colorWithRed:51.0/255.0 green:153.0/255.0 blue:204.0/255.0 alpha:1.0];
        [masterProName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        masterProName.lineBreakMode = NSLineBreakByCharWrapping;
        [myButton addSubview:masterProName];
        [masterProName release];

        UILabel *masterProID = [[UILabel alloc] initWithFrame:CGRectMake(28, 45, 200, 21)];
        masterProID.text = [masterProjIDArray objectAtIndex:i];//@"133 FS";
        masterProID.backgroundColor = [UIColor clearColor];
        masterProID.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];;
        [masterProID setFont:[UIFont fontWithName:@"Helvetica" size:17]];
        masterProID.lineBreakMode = NSLineBreakByCharWrapping;
        [myButton addSubview:masterProID];
        [masterProID release];
    }

And UILabel access method after user press the long press button

- (void)longPressTap:(UILongPressGestureRecognizer *)sender
         {
          if ([recognizer.view tag]) {
                        UILabel *view = (UILabel *)recognizer.view;
                    NSLog(@"---%@", view.subviews);

                    for (UILabel *lbl in view.subviews) {
                        if (recognizer.view.tag == view.tag) {
                            NSString *text = view.text;
                            NSLog(@"---%@", text);
                        }
                    }
            }
        }

here am putting the log

"<UIImageView: 0x75b9510; frame = (0 0; 379 100); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x75acef0>>",
            "<UILabel: 0x88c4e50; frame = (28 20; 200 21); text = 'Aux Water Waste Trtmnt'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x88c49c0>>",
            "<UILabel: 0x88c52f0; frame = (28 45; 200 21); text = 'M10000'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x88c5380>>",
            "<UILabel: 0x88c56c0; frame = (30 70; 60 21); text = '**Default**'; clipsToBounds = YES; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x88c5750>>",
            "<UIButton: 0x88c6440; frame = (240 10; 70 80); opaque = NO; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x88c6500>>"

In this log i want to access only "Default" text label

Please give me any suggestion on this.

4

5 回答 5

1

好吧,您需要知道标签的文本中带有“默认”的标签具有哪个值。假设它是 42。那么

UILabel *label = [view viewWithTag:42];

会成功的。无论如何,您的解决方案也应该有效。查看您正在处理的实际标签值。我有一种胆量感觉,recognizer.view.tag 与标签中带有“默认”的标签的值不同。

于 2013-03-05T12:22:09.617 回答
1

您还可以在运行时创建这些插座。

创建一个数组,然后继续添加每个标签的出口。

完成并需要检索它们后,您可以通过数组索引轻松访问。

即使您可以创建一个字典来存储 outlet,在这里您也可以通过键访问标签。

于 2013-03-05T12:24:31.820 回答
0

当您将标签分配给 时UILabel,我们可以从标签中获取标签(如果我们知道,在您的情况下我们知道)。

所以试试这种方式

    for (int i = 0 ; i <[array count]; i++)
    {
        UILabel *lbl = (UILabel*)[self.view viewWithTag:i+1];
        NSLog(@"--- %@", lbl.text);
    }
于 2013-03-05T12:20:56.380 回答
0

获取UILabel基于它的Tag.

int i;.h file,并放入i=1;上面的for循环

for (NSObject *view in self.View.subviews) 
{
    if ([view isKindOfClass:[UILabel class]]) 
    {
        label = (UILabel *)[[self view] viewWithTag:i];
        NSLof(@"%@".label.text);
       // break;
    }
  i++;
}
于 2013-03-05T12:29:38.640 回答
0

首先,你应该检查

 if (lbl.tag == view.tag)

不是

if (recognizer.view.tag == view.tag)

其次,无论如何它都不会起作用。所做的只是识别你所知道的已被触摸的视图。如何将您想要的标签中的标签值设置为唯一编号并进行测试

if (lbl.tag == <unique tag number>)
于 2013-03-05T13:32:08.423 回答