• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

for loop in objective c

how do I output this in using a for loop in objective c?

  [self.window addSubview:button];
    [self.window addSubview:button2];
    [self.window addSubview:button3];
    [self.window addSubview:button4];
    [self.window addSubview:button5];
    [self.window addSubview:button6];
    [self.window addSubview:button7];
    [self.window addSubview:button8];
    [self.window addSubview:button9];
    [self.window addSubview:button10];
0
charmingduck
Asked:
charmingduck
  • 4
  • 3
2 Solutions
 
Hamidreza VakilianSenior iOS DeveloperCommented:
If you need prompt answers you might consider increasing the points to 500 on your questions, although your questions are not difficult but the explanation is somehow difficult since it sounds you are just beginning iOS development.
0
 
charmingduckAuthor Commented:
400 is sufficient, please help.
0
 
Hamidreza VakilianSenior iOS DeveloperCommented:
    NSArray* array = [NSArray arrayWithObjects:button, button2, button3, button4, button5, button6, button7, button8, button9, button10, nil];

    for (UIButton* btn in array) {
        [self.window addSubview:btn];
    }

Open in new window


We should first put all the buttons in an array then use it in the for loop as above.
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
charmingduckAuthor Commented:
and how do I not create 10 buttons manually?
0
 
Hamidreza VakilianSenior iOS DeveloperCommented:
Then you may try this:

for (int i =0; i != 0; i++) {
 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(20, 20 + i*25, 100, 20); // position in the parent view and set the size of the button
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
[self.window addSubview:myButton];
}

Open in new window

0
 
charmingduckAuthor Commented:
hmm, I dont think this code works, and I did try, it didn't.
0
 
Hamidreza VakilianSenior iOS DeveloperCommented:
Sorry missed the "1" character on first line. Try this one:

for (int i =0; i != 10; i++) {
 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(20, 20 + i*25, 100, 20); // position in the parent view and set the size of the button
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
[self.window addSubview:myButton];
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now