简体   繁体   中英

how to access dynamically created uilabel in iphone

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.

Well, you need to know which value the tag has of the lable with "Default" in its text. Let's assume it is 42. Then

UILabel *label = [view viewWithTag:42];

would do the trick. Anyway your solution should work too. Have a look at the actual tag values that you are dealing with. i've got a guts feeling that recognizer.view.tag does not have the same value as the tag of the label with "Default" in it.

You can also create outlets of those at runtime.

Create an array, and go on to add outlets of each label.

Once you are done and need to retrieve them you can easily access by index of arrays.

Even you can create a dictionary for storing outlets, here you can access labels by keys.

As you are assigning tag to UILabel , we can get label from tag (if it known to us, in your case we know).

So give it a try this way

    for (int i = 0 ; i <[array count]; i++)
    {
        UILabel *lbl = (UILabel*)[self.view viewWithTag:i+1];
        NSLog(@"--- %@", lbl.text);
    }

for get UILabel base on its Tag .

Take int i; in .h file , And put i=1; in above for Loop

for (NSObject *view in self.View.subviews) 
{
    if ([view isKindOfClass:[UILabel class]]) 
    {
        label = (UILabel *)[[self view] viewWithTag:i];
        NSLof(@"%@".label.text);
       // break;
    }
  i++;
}

Firstly, you should be checking

 if (lbl.tag == view.tag)

not

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

Secondly, it won't work anyway. All that does is identify the view that has been touched which you know. How about setting the tag value in the label you want to a unique number and testing

if (lbl.tag == <unique tag number>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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