Reputation: 2777
I have a facebook like button in a custom tableview cell. In the class of my tableview I have the following function.
- (IBAction)sendLike:(id)sender
WithString: (NSString *)shareString
andUrl:(NSURL *)shareUrl{
//Do all kinds of things
[fbController setInitialText:shareString];
[fbController addURL:shareUrl];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
}
In my cellForRowAtIndexpath I am trying to call this method in the following way.
NSString *shareString = video.name;
NSURL *shareUrl = [NSURL URLWithString:video.url];
[cell.facebook addTarget:self action:@selector(sendLike: WithString:shareString andUrl:shareUrl)
forControlEvents:UIControlEventTouchUpInside];
But it is complaining that I should put ':' in my @selector. Can anybody help ?
Thanks in advance.
Upvotes: 1
Views: 860
Reputation: 12085
Your problem is that the number and kind of arguments are incorrect in your selector. You can only use targets with the following scheme:
- (void)action;
- (void)action:(id)sender;
- (void)action:(id)sender forEvent:(UIEvent *)event;
You will have to include your information in the sender (the UITableViewCell
class instance) object. You can do that as described in the other answer by Stas, but I would recommend you to create a custom table view cell subclass which adds a property for shareString
and shareURL
. This approach will require more code, but make it much easier to read, maintain and prevents this ugly Obj-C and C mix up.
Upvotes: 0
Reputation: 3557
As its in the apple documentation, the only available specifications for an @selector
in an action are the following methods:
- (void)action;
- (void)action:(id)sender;
- (void)action:(id)sender forEvent:(UIEvent *)event;
As you can see, your selector doesn't match this, because you have multiple arguments, but only the sender id
is allowed.
As a solution, you can probably set a tag
for your button and then handle it in your selector.
Upvotes: 0
Reputation: 6092
I think you invoked the selector in wrong way. You actually should write,
[cell.facebook addTarget:self action:@selector(sendLike: WithString: andUrl:)
forControlEvents:UIControlEventTouchUpInside];
But this doesn't pass arguments for you. If you want to pass arguments, you should call performSelector like
[self performSelector:@selector(sendLike: WithString: andUrl:)
withObject: cell.facebook
withObject: shareString
withObject: shareUrl];
Hope it will help solving your problem.
Upvotes: 0
Reputation: 9935
In your cellForRow
:
//wiring a custom string to a button
objc_setAssociatedObject(yourButton, "theKey", strText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
In your button handler:
NSString *str = objc_getAssociatedObject(sender, "theKey");
p.s don't forget to import runtime library:
#import <objc/runtime.h>
Upvotes: 0
Reputation: 1824
You can try like this
cell.facebook.tag = indexPath.row.
[cell.facebook addTarget:self action:@selector(sendLike:)
forControlEvents:UIControlEventTouchUpInside];
in sendLike event
- (IBAction)sendLike:(id)sender
{
//if you want to fetch data from any list then try
//UIButton *selectedButton = (UIButton*)sender;
//data = [dataList objectAtIndex:selectedButton.tag];
NSString *shareString = video.name;
NSURL *shareUrl = [NSURL URLWithString:video.url];
[fbController setInitialText:shareString];
[fbController addURL:shareUrl];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
Upvotes: 1