Tuesday, November 9, 2010

Draw custom UIButtons from an array


Code:
- (void)fillButtonArrays {
 
 arr_Buttons_Page1 = [[NSMutableArray alloc] initWithObjects:nil];
 
 int i;
 for (i = 1; i < 5; i++) {
  NSString *buttonImage = [[NSString alloc] initWithFormat:@"Button%d.png", i];
  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 60, 60)];
  [button addTarget:self action:@selector(prevButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  [button setImage:[UIImage imageNamed:buttonImage] forState:UIControlStateNormal];
  [arr_Buttons_Page1 addObject:button];
  [buttonImage release];
  [button release];
 }
 
 
}

- (void)drawButtons {

 int i, x;
 CGRect buttonLocation = CGRectMake(5, 5, 60, 60);
 
 for (i = 0; i < 4; i++) {
  if (i == 0) {
   buttonLocation = CGRectMake(5, 5, 60, 60);
   [[arr_Buttons_Page1 objectAtIndex:i] setFrame:buttonLocation];
  }
  if (i > 0) {
   x = ((i + 1) * 5) + (i * 60);
   buttonLocation = CGRectMake(x, 5, 60, 60); //<--- Missing semicolon here
   [[arr_Buttons_Page1 objectAtIndex:i] setFrame:buttonLocation];

// here it shows 2 errors:
// error: expected ']' before 'setFrame'
// error: array subscript is not an integer
// can someone explain??

  }
  
  [self.view addSubview:[arr_Buttons_Page1 objectAtIndex:i]];
 }
}


- (void)viewDidLoad {
 [self fillButtonArrays];
 [self drawButtons];
}

No comments:

Post a Comment

Followers