yuvaraj
yuvaraj

Reputation: 9

how to retrieve the particular label title from dynamically create label in iphone?

I create the 10 labels dynamically in the view controller. When I click the particular label want to get the clicked label title how can i do this any on help me.

Upvotes: 0

Views: 80

Answers (3)

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

You can use UITapGestureRecognizer to find when user click the label as follows:

UITapGestureRecognizer *singleFingerTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
                                            action:@selector(handleSingleTap:)];
    [myLabel addGestureRecognizer:singleFingerTap];
    [singleFingerTap release];

Then in handleSingleTap method you can find which label is tapped:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    UILabel *view = (UILabel *)recognizer.view;
    NSString *text = view.text;
}

Upvotes: 3

Ishu
Ishu

Reputation: 12787

You can sub class UILabel and in -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event, you can returns its text property value.

Upvotes: 1

Pork 'n' Bunny
Pork 'n' Bunny

Reputation: 6731

You can use a tapgesturerecognizer. This will return the coords of of the tap.

You can then test if the coords of the tap were within the bounds of one of your dynamically created labels.

then you can fetch the title with the "text" property of the UILabel.

Upvotes: 0

Related Questions