views:

96

answers:

3

Hey I have these UIView objects in a dictionary I have created as such:

- (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;
}

So now I need to access each member of this array and add them each to a superview, any ideas how i might go ahead and do this?

+2  A: 

If you have NSArray *views = [self createNumberOfViews:10] then use

[(UIView *) addSubview[views objectAtIndex:number]];

That should work. Comment if it doesn't, but this is pretty basic.
Edit: Oops. Didn't quite understand. Fixed up code :P

HiGuy Smith
Not quite. @Ollie asks about adding each view in the array to a superview; you are adding subviews to each view in the array
ohhorob
+6  A: 

Just get the result of that method and enumerate through it:

for (UIView *view in [self createNumberOfViews:42]) {
    [yourSuperview addSubview:view];
}
Chuck
A: 

Hi,

Please try this

for(NSInteger i = 0; i < [viewArray count]; i++)
{
     UIView *view = (UiView*)[viewArray objectAtIndex:i];
     [parentView addObject:view];   // parentView = your super view
}
Reena