How can I pass any parameter in a UiButton action?

I want to pass the URL through the action of the button, because I have 10 buttons that are created using the code dynamically and when clicked, then for each corresponding button a specific URL is assigned. 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 a warning like "unused linkUrl variable".

I studied various articles, but someone said that passing the linke argument is not possible. Anyone can tell me how I can pass the URL for each button action, as well as how to get these values. in the definition of clickMe.

Thanks in advance.

+5
source share
3 answers

Subclass of UIButton:

MyButton.h

@interface MyButton : UIButton
{
     NSString *_linkUrl;
}

@property (nonatomic, retain) NSString *linkUrl

MyButton.m

@implementation MyButton
@synthesize linkUrl = _linkUrl

Pass the url link:

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

linkUrl :

-(void)clickMe:(MyButton *)sender{
      NSString *url = sender.linkUrl;
}
+11

linkurl NSArray;

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

,

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

-(void)clickMe:(UIButton *)sender{
      NSString *url = [linkUrls objectAtIndex:sender.tag];
}
+2

use 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];

}

0
source

All Articles