0

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?

4
  • A quick glance at the documentation for NSArray (and UIView if necessary) will show you the way Commented Jul 8, 2010 at 19:06
  • It is really a matter of style/preference, but unless the returned array will be modified, you should return an immutable array.. return [NSArray arrayWithArray:viewArray];. Otherwise you should change your method declaration to return NSMutableArray. Commented Jul 8, 2010 at 20:42
  • @ohhorob: That really is a matter of preference. The fact that his array is an NSMutableArray is an implementation detail, just like the fact that all NSArrays are actually NSMutableArrays (seriously, check [[NSArray arrayWithObject:@"Hello"] isKindOfClass:[NSMutableArray class]]). His method signature is accurate and his method implementation is efficient, so there's really no need to complicate things or expose private implementation details IMO. Commented Jul 8, 2010 at 22:34
  • I think I was pretty clear that my comment "is really a matter of style/preference"! Personally, my reasoning of why the declaration should "declare" (yes, really!) the actual return type of a mutable/immutable class cluster is it can provide semantics useful to reading and using the code. At the end of the day it's not a big deal, but a useful habit nonetheless. Commented Jul 8, 2010 at 22:51

3 Answers 3

6

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

for (UIView *view in [self createNumberOfViews:42]) {
    [yourSuperview addSubview:view];
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Not quite. @Ollie asks about adding each view in the array to a superview; you are adding subviews to each view in the array
0

Please try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.