Walker
Walker

Reputation: 1225

Loop through similarly-named Controls

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];
}

Upvotes: 2

Views: 650

Answers (9)

Rogers
Rogers

Reputation: 1973

Assuming Interface Builder (otherwise straightforward as answered before). This is the best I could manage:

In .h:

{
    UILabel         *banner[NUM_BANNERS];
}
@property (nonatomic, retain) IBOutlet UILabel *banner0;
@property (nonatomic, retain) IBOutlet UILabel *banner1;
@property (nonatomic, retain) IBOutlet UILabel *banner2;
etc.

In .m:

- (void)viewDidLoad
{
    banner[0] = banner0;
    banner[1] = banner1;
    banner[2] = banner2;
    etc.

Then can access by array index. Remember to release.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96323

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.

Upvotes: 3

Bill K
Bill K

Reputation: 62759

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

Upvotes: 1

Lounges
Lounges

Reputation: 4664

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

- (id)viewWithTag:(NSInteger)aTag

Upvotes: 3

Dinah
Dinah

Reputation: 54017

Either use a collection or Reflection.

Upvotes: 0

Ben Gotow
Ben Gotow

Reputation: 14884

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.

Upvotes: 0

teabot
teabot

Reputation: 15444

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];
}

Upvotes: 1

richardtallent
richardtallent

Reputation: 35363

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

Upvotes: 0

Marco Mustapic
Marco Mustapic

Reputation: 3859

Put the views in an array at startup.

Upvotes: 7

Related Questions