views:

100

answers:

2

I know this question was already asked in the past, but i am really confused and can't get out of it.

i have got 9 pointers to IB objects declared like:

IBOutlet UIButton *but1;

IBOutlet UIButton *but2;

....

NSMutableArray *buttons;

declared properties:

@property (nonatomic,retain) IBOutlet UIButton *but1;

@property .....

in IB i have made the connections from buttons to pointers.

I need now to fill the array buttons with these pointers to run UIButton methods like

[[buttons objectWithId:0] setImage:[UIImage imageNamed:@"image1.png"]];

I know i can't fill arrays with pointers, is there some way to get IBOutlets into arrays?

+1  A: 

You can't fill Cocoa containers with arbitrary pointers, but pointers to objects of class type - like your UIButtons - are fine.
Cocoa containers retain and release the objects added to them, so pointers to non-class types would have to be wrapped with e.g. NSValue.

Also note that IBOutlet is no type, it is an empty macro that is just used to mark up code parts for Interface Builder.

Georg Fritzsche
ok, got it, but i still don't understand how to solve my problem, what should I insert into the array to get it work?
palominoz
A: 

Your overthinking the problem. By default, all references to all objects are pointers.

This:

IBOutlet UIButton *but1;

... defines a pointer.

Therefore, all default operations with objects use pointers. You usually don't even have to think about it. To add a pointer to a UIButton to a NSMutableArray just use (note the self notation):

[self.buttons addObject:self.but1];

The IBOutlet designation has not effect on references in code. It is just there so Interface Builder can parse source and figure out which properties is supposed pay attention to. Otherwise, it does nothing and you can ignore it.

Of course, I have to ask: Why are you putting buttons that are already named properties into an array?

Instead of this:

[[buttons objectWithId:0] setImage:[UIImage imageNamed:@"image1.png"]];

... why not just do this?

[self.but1 setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal]; 

There's not much sense in using named properties if you don't use the names as the primary reference.

TechZen
of course if i had only one button i would have done like you suggested.the fact is i don't want the code to be long and difficult to read, so managing buttons with arrays is what I want to do.your idea is great and would help me a lot, but i cannot link IB objects to the code if I miss properties, thats why i am declaring 9 times an object!
palominoz