-1

I have some objects named object1, object2, object3, and I'd like to send the same message to them with a for loop, instead of :

[object1 message];
[object2 message];
[object13 message];
...
[object200 message];

Thanks.

Edit : Sorry guys, my question wasn't precise (and maybe my english too...)

I have some objects (more precisely custom buttons) in my interface and the same number of custom views. When i touch a button, it send the same message to the related view.

In the view controller header file, i declared n outlets for my views, and n actions for my buttons.

So my question is : instead of declaring all theses outlets and actions (a lot of copying/pasting and editing...), how can I code just one action, and send the message to the good object.

Thanks for your answers!

3 Answers3

3

You can put them into array and use NSArray's method makeObjectsPerformSelector:

[array makeObjectsPerformSelector:@selector(message)];
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
2

Just throw them into an array:

NSArray *myArray = @[object1, object2, ... object200];

Then you can either loop through them:

for (ClassObject *i in myArray) {
    [i message];
}

or easier just use makeObjectsPerformSelector:

[myArray makeObjectsPerformSelector:@selector(message)];
Firo
  • 15,448
  • 3
  • 54
  • 74
  • 2
    Honestly, if the objects are named `object1` through `object200`, chances are that they should be in an array already. If not, OP has a much larger problem than having to send one message to 200 objects. – JustSid Nov 26 '13 at 18:17
0

Register your objects to listen for a specific NSNotification sent from an NSNotificationCenter. Once heard, call your message on the object. If all your objects are children of the same parent class, you can set this up in the class definition.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345