views:

196

answers:

8

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];
}
+6  A: 

Put the views in an array at startup.

Marco Mustapic
A: 

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).

richardtallent
+1  A: 

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];
}
teabot
A: 

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.

Ben Gotow
If you're correct you certainly deserve a +1 for that!
teabot
Sorry - looks like I was wrong. The Interface Builder docs say "Only one object at a time may be connected to a given outlet." I swear I've done this somehow - so I'll keep looking. Sorry for the false answer though.
Ben Gotow
Ben, you're probably better off adding your comment as an edit to your answer.
Abizern
A: 

Either use a collection or Reflection.

Dinah
+3  A: 

You could put a tag on each view and use a for loop with the following method:

- (id)viewWithTag:(NSInteger)aTag
Lounges
+1  A: 

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).

Bill K
That's not quite what I want here- I only wanted certain views.But that is an interesting idea. How did you get an array of controls?
Walker Argendeli
I'm not sure in Objective-C... Java every panel has a getControls() method.
Bill K
+3  A: 

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.

Peter Hosey