5

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.

MathieuF
  • 3,130
  • 5
  • 31
  • 34
Banshi
  • 1,335
  • 2
  • 13
  • 21

3 Answers3

11

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;
}
MathieuF
  • 3,130
  • 5
  • 31
  • 34
  • can we not pass argument dynamically through action selector according to my requirement? Like that NSString *linkUrl = [NSString stringWithFormat:@"%@",[AllrecordG objectForKey:@"ColorLink"]]; [leftLabelG addTarget:self action:@selector(clickMe:linkUrl:) forControlEvents:UIControlEventTouchUpInside]; – Banshi May 14 '12 at 09:01
  • Actually my page class is already @interface homePage : UIViewController so how can i implement your above code – Banshi May 14 '12 at 09:08
  • You can keep your subclass of UIViewController with a subclass of UIButton. Change the button's declaration like this : MyButton *leftLabelG; – MathieuF May 14 '12 at 09:14
  • Sir I have tried to implement UIButton to my homePage class but all types of writing showing syntax error can you please help me to write the proper syntax of my above requirement!!!! – Banshi May 14 '12 at 10:15
2

put your linkurl in a NSArray;

NSArray *linkUrls = [NSArray arratWithObjects:@"link",@"blah",@"blah"........,nil];

then set a tag to your button as the index in the array,then

leftLabelG.tag = 1;
rightLabelG.tag = 2;

-(void)clickMe:(UIButton *)sender{
      NSString *url = [linkUrls objectAtIndex:sender.tag];
}
adali
  • 5,977
  • 2
  • 33
  • 40
0

use a category

- (void)addTarget:(id)target action:(SEL)action withObject:(id)object forControlEvents:(UIControlEvents)controlEvents;


-(void)addTarget:(id)target action:(SEL)action withObject:(id)object1 withObject:(id)object2 forControlEvents:(UIControlEvents)controlEvents {
SEL action_ = [self generateAction:action withObject:object1 withObject:object2];
IMP impl = imp_implementationWithBlock(^(id self_) {
    objc_msgSend(self_, action, self, [object1 copy], [object2 copy]);
});
class_replaceMethod([target class], action_, impl, "v@:@");

[self addTarget:target action:action_ forControlEvents:controlEvents];

}

SleepsOnNewspapers
  • 3,353
  • 3
  • 23
  • 29