Creating views on the fly from array
I need to create a a bunch a views on the fly. I was wondering what would be the best way to go about doing this as ill need to defin开发者_Go百科e coordinates, tag, colours of each view.
Would i need to create a multidimensional array and if so how can i do this?
CGRect viewRect1 = { 80.0, 200.0, 160.0, 100.0 };
UIview *myview1 = [[UIview alloc] initWithFrame:viewRect1];
[myview1 setBackgroundColor:[UIColor darkGrayColor]];
You need to define a structure to hold the data which is needed to create your UIView
.
@interface MyViewDataHolder :NSObject
{
CGRect mViewRect;
UIColor* mDarkGrayColor;
NSInterger mTag;
}
@end
Then create an object of above class AND assign the values in member then add that in your NSArray
...
EDITED:
In MyViewDataHolder.h class
@interface MyViewDataHolder :NSObject
{
CGRect mViewRect;
UIColor* mDarkGrayColor;
NSInteger mTag;
}
@property (nonatomic,assign) CGRect mViewRect;
@property (nonatomic,retain) UIColor* mDarkGrayColor;
@property (nonatomic,assign) NSInteger mTag;
@end
In MyViewDataHolder.mm class
#import "MyViewDataHolder.h"
@implementation MyViewDataHolder
@synthesize mViewRect,mDarkGrayColor,mTag;
-(void) dealloc
{
[mDarkGrayColor release]
mDarkGrayColor = nil;
}
Now How to use it ....
Create objects of MyViewDataHolder
like below ...
MyViewDataHolder* myObj1 = [[MyViewDataHolder alloc] init];
myObj1.mViewRect = CGRectMake(x,y,width,height);
myObj1.tag = 1;
myObj1.mDarkGrayColor = [UIColor redColor];
Create as much as you need
Then Create an NSMutableArray
and add each objects of MyViewDataHolder
into NSMutableArray
.
NSMutableArray* myArray = [[NSMutableArray alloc] init];
[myArray addObject:myObj1];
[myArray addObject:myObj2];
[myArray addObject:myObj3];
and So on ....
When you need the stored info you could use as below ...
for(int index =0; index < [myArray count]; index++)
{
MyViewDataHolder* myObj = (MyViewDataHolder*)[myArray objectAtIndex:index];
myView = [[UIView alloc] initWithFrame:myObj.mViewRect];
//incriment x and y to refelect where you want your next view to be suituated
myView.tag = myObj.mTag;
myView.backgroundColor = myObj.mDarkGrayColor;
[self.view addSubview:myView];
[myView release];
}
The code reflect the approach , Although I didn't compile the code So use it as reference
Thanks ,
You dont really need to store your views in an array unless its just for pure convenience.
You can create a variable amount views using a for loop:
UIView *myView;
int x=0, y=0;
int viewWidth = 50, viewHeight = 50;
for(int i=0; i<numberOFView; i++){
myView = [[UIView alloc] initWithFrame:CGRectMake(x,y,viewWidth,viewHeight)];
//incriment x and y to refelect where you want your next view to be suituated
myView.tag = i+1;
[self.view addSubview:myView];
[myView release];
}
Then to access each view in turn and carry out some work you can do the following:
for (UIView *view in self.view.subviews) {
switch (view.tag) {
case 1:
//do something to the view
break;
case 2:
//do something to the view
break;
case 3:
//do something to the view
break;
//and so on...
default:
break;
}
}
精彩评论