2

This is my code, which occurs 48 times (one for each button in a calendar).

calenderButton *a1 = [calenderButton buttonWithType:UIButtonTypeCustom];
[a1 makeFrame:CGRectMake(75, 50, 70, 70) number:1 color:[UIColor orangeColor]];
[self.view addSubview:a1];

What I want to do is put this in a "for loop", changing the "a1" to "a2", "a3", etc, to cut down on the amount of code. I figure I can cut it down to 6 "for loops".

How do I get the "a1"s to be a variable that I can then reference in not only the code above but in the "for loop"? (the for loop would be something like this:)

for(int i = 0, j=75; i < 7; i++, j+=75);

I know I have to concatenate the "a" with the integer "i", but how do I place that in the message?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • why are you trying to do this? How is it more beneficial than having an array(s) of the objects? – ColdLogic Mar 27 '12 at 16:03
  • There's also http://stackoverflow.com/q/2283374/, http://stackoverflow.com/q/7758757/, http://stackoverflow.com/q/7601937/, http://stackoverflow.com/q/8090590/, and others linked from those. – jscs Mar 27 '12 at 17:12

2 Answers2

3

Your code will create 48 different buttons even if you use the same (local) variable:

for (int i = 0; i < 48; i++){
    calenderButton *a1 = [calenderButton buttonWithType:UIButtonTypeCustom];
    [a1 makeFrame:CGRectMake(75, 50, 70, 70) number:1 color:[UIColor orangeColor]];
    [self.view addSubview:a1];
}

If you want to keep a reference to the buttons, you can store them in an array:

self.buttons = [NSMutableArray array];
for (int i = 0; i < 48; i++){
    calenderButton *a1 = [calenderButton buttonWithType:UIButtonTypeCustom];
    [a1 makeFrame:CGRectMake(75, 50, 70, 70) number:1 color:[UIColor orangeColor]];
    [self.view addSubview:a1];
    [self.buttons addObject:a1];
}

And access them later like this:

calenderButton *button = [self.buttons objectAtIndex:7]; // Or any other index

Note, you can use the loop you mentioned:

for(int i = 0, j=75; i < 7; i++, j+=75)

But I am not sure this will produce 48 buttons.

sch
  • 27,436
  • 3
  • 68
  • 83
  • sch: how does it add 48 DIFFERENT buttons? each has to have a unique identifier so I can reference it... in this case, they are all called "a1", right? – SpokaneDude Mar 27 '12 at 16:06
  • 1
    @spokane-dude: actually, no. This adds 48 different buttons because you create 48 different buttons. The name isn't unique in the global scope, only in the scope of the loop. To reference a button you would use `[[self buttons] objectAtIndex:7];` This would essentially be the a7 button you are looking for, but it wouldn't be available via the `a7` pointer – ColdLogic Mar 27 '12 at 16:12
  • That's ok that I can't reference via a7 pointer... I need to have the ability to add and remove objects (labels and symbols) from any of the generated buttons... – SpokaneDude Mar 27 '12 at 16:14
  • As @ColdLogic said, you can reference them using their index in the array. – sch Mar 27 '12 at 16:16
  • An extra tip: when dealing with a bunch of similar views (like your calendar setup), it's often useful to group them in a containing view and give each a unique integer tag -- then you can refer to them with `viewWithTag:`. – rickster Mar 27 '12 at 16:54
3

You can put your buttons into an array, like this:

Declare an instance variable NSMutableArray *allButtons in your header, then...

allButtons = [NSMutableArray array];
for(int i = 0, j=75; i < 7; i++, j+=75) {
    calenderButton *cb= [calenderButton buttonWithType:UIButtonTypeCustom];
    // Configure the button here...
    // use values of i and j to call CGRectMake, or as you see fit
    [allButtons addObject:cb];
}

Now all your buttons are in an array. You can access them by index or in any other way that you may need, e.g. with a fast enumeration loop.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523