views:

39

answers:

2

Hi all:

I have a predicament, in as much that I need to create an arbitrary amount of UIView Objects. I have an NSArray and what I need to do is create UIView Objects for the number of items in the array, so I got an int from the [NSArray count]; method, so I know the number of objects needing creating, but the way to implement this has me stumped. I'll include some psudocode below to attempt to give across what I need to do:

[UIView returnMultipleUIViewsForInt:[theArray count]];

Obviously that won't work, but some way of creating an arbitrary amount of objects at runtime, which I can work with would be good.

So in short:

I need to create a certain number of UIViews based upon the number of items in an array. I then need to access each view that is created and use it as a regularly created view might be used, doing things like adding one of them as a subview to a different view.

+1  A: 
- (NSArray *)createNumberOfViews:(NSInteger)number
{
    NSMutableArray *viewArray = [NSMutableArray array];
    for(NSInteger i = 0; i < number; i++)
    {
        UIView *view = [[UIView alloc] init];
        // any setup you want to do would go here, e.g.:
        // view.backgroundColor = [UIColor blueColor];
        [viewArray addObject:view];
        [view release];
    }
    return viewArray;
}
Noah Witherspoon
That's great! However, I Still need to access each view in this array, so how might one go about say adding each view in the new array of views as a subview of another view, or just access each view in this array as a view object...
Ollie
@Ollie: please read: 1. the answer again, 2. NSArray documentation, 3. the chapter on loops on any programming book.
Can Berk Güder
A: 
NSMutableArray *newViews = [NSMutableArray array];
for (int i=0; i<[theArray count]; ++i) {
    UIView *view = [[UIView alloc] init];
    [newViews addObject:view];
    [view release];
}
BJ Homer