I have view0 through view25. I don't particularly want to have a 25-case switch, so is there a way to do something like this?
- (void)modifyViewNumber:(int)number
{
[view*number* dosomething];
}
I have view0 through view25. I don't particularly want to have a 25-case switch, so is there a way to do something like this?
- (void)modifyViewNumber:(int)number
{
[view*number* dosomething];
}
Create an array of views. Add each one, in order, then reference it by the array index.
If the numbers are not sequential, use a hash table.
Reflection might be an option, but could be slower (I'm not an Objective C guru, don't even know if that is practical).
Why don't you put the views into an array and obtain references to them using the array index?
- (void)modifyViewNumber:(int)number
{
UIView* view = [views objectAtIndex:number];
[view dosomething];
}
Hey Walker,
I assume you're using Interface Builder to setup those views?
Someone may have to correct me on this, but I believe you can create an NSArray Interface Builder outlet in your class and then assign all your views to it. For example, you could declare "IBOutlet NSArray * views;" in your header file, and then use interface builder bindings to tie all 25 views to that property. They'll automatically be added to the array, and then you can cleanly iterate over it in your code.
You could put a tag on each view and use a for loop with the following method:
- (id)viewWithTag:(NSInteger)aTag
Many people have said use an array--I do this all the time, it's really the only way to go when using an interface builder, but I wanted to add a little.
Sometimes getting the values into the array can be tricky. There is usually a way to get an array of all controls on the screen. usually I'll grab that and iterate over it looking for a type of control with a certain naming pattern and collect those into my own array.
In this way, you can often add a new control with no code change whatsoever (always one of my favorite goals).
Nobody's suggested NSMatrix yet? This sounds like what it's made for.
The caveat is that the views must all be controls, such as text fields or buttons. Basically, look at its class reference and see whether it inherits from NSControl at some point. If it passes that test, then NSMatrix is an option. (Here's the list of all AppKit classes, to make this easy.)
To make a control into a matrix in IB, create one of the control in the usual way, then option-drag its resize handle. It won't appear to do anything at first, but keep dragging. Instead of resizing, your control will proliferate; it is now a matrix of cells instead of a single control.