This is a very elemental question about object-orientated programming- so sorry in advance if this is obvious.
I have a view and add several objects to this view (as subviews). I do this programatically. Every object get’s an individual ID so that I can later tell objects apart (in this case it is the int position). This is my code:
for (int i=0; i < [directories count]; i++) {
NSLog(@"%i", i);
Notebook *aBook = [[Notebook alloc] initWithName:aName withPosition:i];
[self.view addSubview:aBook];
[aBook release];
[aName release];
}
After this, a have a view with several objects attached to it. My question is how I can get to these objects now. What if I wanted to find out the name or any other variable saved in this object? E.g. how would I get the nameOfBook if I knew what the stackPosition is? Here is the header of my object notebook:
#import <Foundation/Foundation.h>
@interface Notebook : UIView
{
NSString *nameOfBook;
int stackPosition;
int bookWidth;
int bookHeight;
int xPosition;
int yPosition;
}
@property (nonatomic, retain) NSString *nameOfBook;
@property int stackPosition;
@property int bookWidth, bookHeight, xPosition, yPosition;
-(id)initWithName:(NSString *)name withPosition:(int)position;
-(void)putOnScreen;
@end
You can create an array or dictionary of your Notebook objects and add each one into there. Then you can reference them later on by getting the array / dictionary object.
So create instance var for your array….
And then add your objects into the array in your loop….
And when you want to retrieve it later (using 0 based index)….