Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i pass any kind of parameter in UiButton action?

I want to pass a url through button action, because i have 10 buttons which are created through code dynamically and on clicking then there a specific url are assign for each corresponding button. Here is my code

NSString *linkUrl = [NSString stringWithFormat:@"%@",[AllrecordG objectForKey:@"Link"]];
[leftLabelG addTarget:self action:@selector(clickMe:linkUrl:) forControlEvents:UIControlEventTouchUpInside];

-(void)clickMe:(UIButton *)sender{
}

But here I get warning as "unused variable linkUrl".

I have studied different articles but somebody told that it is not possible to pass argument linke that. Anybody can tell me that how can i pass url for each button action and also how I retrieve those values. in the clickMe definition.

Thanks in advance.

like image 609
Banshi Avatar asked May 14 '12 08:05

Banshi


1 Answers

Subclass UIButton like that :

MyButton.h

@interface MyButton : UIButton
{
     NSString *_linkUrl;
}

@property (nonatomic, retain) NSString *linkUrl

MyButton.m

@implementation MyButton
@synthesize linkUrl = _linkUrl

Pass the linkUrl :

NSString *linkUrl = [NSString stringWithFormat:@"%@",[AllrecordG objectForKey:@"Link"]];
[leftLabelG addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];
[leftLabelG setLinkUrl:linkUrl];

Now you can get the linkUrl in your action like that :

-(void)clickMe:(MyButton *)sender{
      NSString *url = sender.linkUrl;
}
like image 118
MathieuF Avatar answered Oct 21 '22 18:10

MathieuF