Suggestions, working with multiple controls(UIButtons)
I'll try to dive right to the point...
I want to build an application that开发者_C百科 will have over 500 controls (probably way more than that)and I am trying to devise ways of how I can build this as efficiently as possible. Quick overview:
Each control(suppose it's a UIButton, it could be something else)is going to have a color attached to it. They will be predefined colors, so a color wheel won't be of use to me for this application. So I have say, black, red,blue...and 500 others. When I click on the control, it will change the color of my background. Now, I don't think I should be creating a control manually in IB to acommodate each and every color. Somehow that manual process seems archaic and not very efficient.
What would you guys recommend I should do? I'm sure some of you how come across something like this, and those who haven't probably have a good idea of how to implement, or at least experiment with it.
I am open to suggestions :)
Something i have done is build up a grid of circa 250 buttons dynamically and dropped them into a scroll view in a popover. The only element that was in the xib was a scrollview embedded in a view. And thats because im a lazy mofo and didnt want to create the scroll view on the fly. When you know the final content size of your grid pass it to the scrollview
You can subclass UIButton and add a UIColor property or some other property that works for your system. Point the UIButtons touchUp action at a single selector on your controller and interrogate the sender for its related property . So your Red button might have [UIColor redColor]
as its property.
Another way is to keep an array on the controller of your color objects and set the button tag as the array index. It means you dont have to subclass UIButton.
Assuming this will be constructed on the fly when you popover/push the view controller memory wont be an issue as you will be throwing it all away on dismiss/pop.
Try this.
-(void)viewDidLoad
{
[super viewDidLoad];
//collect all the objects
NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"shapes"];
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[array count]];
for(NSString *path in array)
{
[temp addObject:[path lastPathComponent]];
}
shapeArray = [[NSArray arrayWithArray:temp] retain];
//compose into a grid of buttons
CGRect final = CGRectZero;
NSInteger count = 0;
NSInteger rowsize = 6;
for (NSString *shape in shapeArray) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *ipath = [[NSBundle mainBundle] pathForResource:[shape stringByDeletingPathExtension] ofType:[shape pathExtension] inDirectory:@"shapes"];
UIImage *image = [UIImage imageWithContentsOfFile:ipath];
[button setImage:image forState:UIControlStateNormal];
div_t temp = div(count, rowsize);
button.frame = CGRectMake(48 * temp.rem , 48*temp.quot, 48, 48);
button.tag = count++;
button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:@selector(pickShape:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:button];
final = CGRectUnion(final, button.frame);
}
scroller.contentSize = final.size;
}
You can use IBOutletCollection. Its for similar requirements. I will try to paste a blog post which explained it in detail.
Some related SO posts are: Practical efficient usage of IBOutletColletion
精彩评论