Ollie Hirst
Ollie Hirst

Reputation: 592

Using objects in an NSArray

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?

Upvotes: 0

Views: 778

Answers (3)

Reena
Reena

Reputation: 845

Please try this

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

Upvotes: 0

Glenn Smith
Glenn Smith

Reputation: 912

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

Upvotes: 2

Chuck
Chuck

Reputation: 237010

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

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

Upvotes: 6

Related Questions