Ollie Hirst
Ollie Hirst

Reputation: 592

Dynamic UIViews in Obj-c + Cocoa touch

I need a way of creating a UIView dynamically. Thus, the parent class could look for say a count of array items, and create UIViews on the fly of the amount of items in the array.

The views need to be allocated dynamically, do I can't create them on the fly.

Can you help?

Upvotes: 0

Views: 661

Answers (2)

John Riselvato
John Riselvato

Reputation: 12924

I think what you want is this (according to the response on taskinoor answer).

NSMutableArray* array = [[NSMutableArray alloc] init];
for (int i =0; i < 10; i++){
   UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 5.0 + (i * 20), 15.0, 15.0)];
   [array addObject:myView];    

}

When when you want one of those views just call

UIView* view = (UIView*)[array objectAtIndex:number];

Upvotes: 0

taskinoor
taskinoor

Reputation: 46037

You can create them anytime.

UIView *myView = [[UIView alloc] initWithFrame:myFrameRect]

You can call this from within a loop, store the pointer in another array for further use or whatever you want. Just remember to release them when you are done. Otherwise you will be in a memory issue.

Upvotes: 2

Related Questions