0

I need to pass parameters with @selector and here is the method that i need to call using selector:

-(void)clickedInfo:(NSString *)itemIndex{
// some work with itemIndex
}

I know that what i can do is to use an intermediate method as described here.

This approach doesn't work in my case because im adding the target to the uibutton in the cellForItemAtIndexPath method for the collectionView.

The parameter that i need to pass to the clickedInfo method is indexPath.row and i can not obtain this parameter in an intermediate method.

Thanx in advance

Community
  • 1
  • 1
onuryilmaz
  • 378
  • 1
  • 4
  • 9

3 Answers3

3

So you want to store some information that can be accessed by the action of a button. Some options are:

  • Use the tag property of the control. (can only store an integer)
  • Subclass UIButton and use that class for the button. The class can have a field that stores the information.
  • Use associated objects (associative references) to attach an object to the button. This is the most general solution.
newacct
  • 119,665
  • 29
  • 163
  • 224
  • The tag property seems like a good idea for what he wants. Just set the tag to `indexPath.row` instead of passing that as a parameter. – Eric Amorde May 30 '13 at 22:48
  • i used the titlelabel field of the uibutton to store string value since i dont have to display anything using that. thanx – onuryilmaz May 31 '13 at 11:07
2

You can use the performSelector:withObject: selector to pass an object.

Example:

[self performSelector:@selector(clickedInfo:) withObject:myIndex];

- (void) clickedInfo:(NSString *)itemIndex{
// some work with itemIndex
}

Edit: Should be just @selector(clickedInfo:) rather than what I had before.

Edit: Using @newacct 's suggestion, I'd recommend doing something similar to the following:

- (UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath
{
    button.tag = indexPath.row;
    [button performSelector:@selector(clickedInfo:)];
    // or
    [button addTarget:self action:@selector(clickedInfo:) forControlEvents:UITouchUpInside];
}

- (void) clickedInfo:(id)sender
{
    int row = sender.tag;
    // Do stuff with the button and data
}
Eric Amorde
  • 1,068
  • 9
  • 19
  • You're right it was incorrect, updated my answer but someone else has already given the same example. – Eric Amorde May 30 '13 at 20:16
  • the question indicates they want to set up the action for a button – newacct May 30 '13 at 22:18
  • Well would the button be holding the object? Otherwise what he wants isn't really possible. I thought he said he was doing this in the `cellForRowAtIndexPath` method. – Eric Amorde May 30 '13 at 22:47
  • @EricAmorde: Yes, he is doing this in `cellForRow...` to set up a cell, which probably contains a button on which he wants to set up the action. – newacct May 31 '13 at 02:59
  • i did what you explained in a bit difference, difference is that i used titlelabel of the uibutton not the tag since i needed to store a string. your code helped thanx – onuryilmaz May 31 '13 at 11:10
  • Ah, that works too as long as that's what you want the title of the button to be. Glad you figured that out! Thanks to @newacct for the suggestions. – Eric Amorde May 31 '13 at 15:24
1

this is addressed lots of places, but it is easier to answer than to point you there:

[someObject performSelector:@selector(clickedInfo:) withObject:someOtherObject];

where someObject is the receiver and someOtherObject is the parameter passed to clickedInfo

Grady Player
  • 14,399
  • 2
  • 48
  • 76