Базовый массив Objective C для поддержки цикла - PullRequest
1 голос
/ 23 января 2011

Итак, вот мое зависание, я создаю массив, передаю в него кнопки и использую цикл for, пытаясь разместить каждую кнопку в представлении.Я знаю, что они будут в одинаковом положении, потому что они оба проходят через один и тот же CGRectMake.Однако я не могу разместить что-либо на экране с этим кодом, хотя он не возвращает никаких ошибок, но все равно меня озадачивает, почему.Спасибо!

- (void)practiceMethod{

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Practice Method has begun" message:@"Alert" delegate: self cancelButtonTitle:@"Close" otherButtonTitles: nil];
 //[alert show];
 [alert release];

 float i=0;

 CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

 UIImage *buttonGraphic = [UIImage imageNamed:@"1.png"];

 UIButton *buttonOne = [[UIButton alloc] initWithFrame:applicationFrame];
 UIButton *buttonTwo = [[UIButton alloc] initWithFrame:applicationFrame];

 buttonArray = [[NSArray alloc] initWithObjects:buttonOne, buttonTwo,nil];

 for (i = 0; i > [buttonArray count]; i++) {

  UIButton *tempElement = [buttonArray objectAtIndex:i];

  tempElement.frame = CGRectMake(140, 230, 50, 50);

  [tempElement setBackgroundImage:buttonGraphic forState:UIControlStateNormal];

  [self addSubview:tempElement];
 }
}

Ответы [ 2 ]

8 голосов
/ 23 января 2011

Изменение

for (i = 0; i > [buttonArray count]; i++) {

до

for (i = 0; i < [buttonArray count]; i++) {

и вы будете в рабочем состоянии.

PS. Я предлагаю вам использовать Objective-Cs for each итератор для решения таких проблем:

for( UIButton* button in buttonArray )
{ 
    // stuff with button
}
3 голосов
/ 23 января 2011

Заменить:

for (i = 0; i > [buttonArray count]; i++) {
   UIButton *tempElement = [buttonArray objectAtIndex:i];
   ...

}

С синтаксисом зацикливания массива:

for (UIButton *tempElement in buttonArray) {
    ...

}

Также лучше использовать buttonWithType: с UIButton:

UIButton *buttonOne = [UIButton buttonWithType: UIButtonTypeCustom];
buttonOne.frame = applicationFrame;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...