1

Is there any provision in objective c so that I can call same selector on multiple object.
Some thing like jQuery does. I referred this link but it requires all objects in array to invoke selector makeObjectsPerformSelector.
For example I have following code:

[_addButton setTarget:myObject];
[_deleteButton setTarget:myObject];
[_editButton setTarget:myObject];

to a line some thing like

[_addButton, _deleteButton, _editButton setTarget:myObject];

I know it is syntactically not making any sense on objective-c, but is there any trick ?

Community
  • 1
  • 1
Kaunteya
  • 3,107
  • 1
  • 35
  • 66

2 Answers2

2

I prefer blocks.

[@[self.addButton, self.deleteButton, self.editButton] enumerateObjectsUsingBlock:^(UIButton* btn, NSUInteger idx, BOOL *stop){
    btn.target = myObject;
}];
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22
1

You can easily put your objects in a temporary array and use makeObjectsPerformSelector like this:

[@[_addButton, _deleteButton, _editButton] makeObjectsPerformSelector:@selector(setTarget:) withObject:myObject]
sgvd
  • 3,819
  • 18
  • 31