开发者

NSMutableArray filled with pointers

I know this que开发者_StackOverflowstion 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜