1

I know there is a better way to do this than my noob implementation. I'm trying to add 4 buttons to a view with 4 different CGrects so that the buttons stack one on top of the other. The code works fine now, but I'm struggling to turn this duplicated code into a working method:

CGRect rect = CGRectMake(30.0f, 250.0f, 250.0f, 250.0f);
CGRect rect1 = CGRectMake(30.0f, 275.0f, 250.0f, 250.0f);
CGRect rect2 = CGRectMake(30.0f, 300.0f, 250.0f, 250.0f);
CGRect rect3 = CGRectMake(30.0f, 325.0f, 250.0f, 250.0f);

enableAlarm = [[UIButton alloc] initWithFrame:rect];
[enableAlarm setTitle:firstTime forState:UIControlStateNormal];
[enableAlarm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm];

enableAlarm1 = [[UIButton alloc] initWithFrame:rect1];
[enableAlarm1 setTitle:secondTime forState:UIControlStateNormal];
[enableAlarm1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm1];

enableAlarm2 = [[UIButton alloc] initWithFrame:rect2];
[enableAlarm2 setTitle:thirdTime forState:UIControlStateNormal];
[enableAlarm2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm2];

enableAlarm3 = [[UIButton alloc] initWithFrame:rect3];
[enableAlarm3 setTitle:fourthTime forState:UIControlStateNormal];
[enableAlarm3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[self view] addSubview:enableAlarm3];

I feel like this can be put into one simple method that I can call four time but can't figure out how to do it ...

kevingduck
  • 531
  • 1
  • 6
  • 21
  • If you're just starting out with programming, finding a good book or a series of online tutorials will probably serve you better than questions on Stack Overflow. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Aug 27 '14 at 22:01

1 Answers1

4

everytime you are repeating something, try loop

CGRect rects = { {30.0f, 250.0f, 250.0f, 250.0f},
                {30.0f, 275.0f, 250.0f, 250.0f},
                {30.0f, 300.0f, 250.0f, 250.0f},
                {30.0f, 325.0f, 250.0f, 250.0f},
                };
NSString *titles = @[firstTime, secondTime, thirdTime, fourthTime];

NSMutableArray *enableAlarmButtons = [NSMutableArray array]; // if you want to access these buttons later
for (int i = 0; i < 4; ++i) { // you can use sizeof to avoid hardcode number
    UIButton  *enableAlarm = [[UIButton alloc] initWithFrame:rects[i]];
    [enableAlarm setTitle:titles[i]  forState:UIControlStateNormal];
    [enableAlarm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [[self view] addSubview:enableAlarm];
    [enableAlarmButtons addObject:enableAlarm];
}

if only y value changes

CGRect rect = CGRectMake(30.0f, 250.0f, 250.0f, 250.0f);
NSString *titles = @[firstTime, secondTime, thirdTime, fourthTime];

NSMutableArray *enableAlarmButtons = [NSMutableArray array]; // if you want to access these buttons later
for (int i = 0; i < 4; ++i) {
    UIButton  *enableAlarm = [[UIButton alloc] initWithFrame:rects[i]];
    [enableAlarm setTitle:titles[i]  forState:UIControlStateNormal];
    [enableAlarm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [[self view] addSubview:enableAlarm];
    [enableAlarmButtons addObject:enableAlarm];

    rect.origin.y += 25;
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • Beat me to it. Note that the title also changes (`firstTime`, `secondTime`, etc.); you'll need an array or something for those as well. – jscs Aug 27 '14 at 22:03
  • Also, since the y value is the only change -- and changes constantly -- between the rects, you could simplify that by just doing addition at each iteration. – jscs Aug 27 '14 at 22:04
  • Thanks, Bryan. This is very helpful and makes perfect sense. – kevingduck Aug 27 '14 at 22:17