Objetivo C: agregar acción en matriz de botones

Soy un novato en Objective-C ¿alguien puede ayudarme aquí? Estoy tratando de hacer un carrusel de button, pero no pude hacerlo.

Lo que tengo es solo mostrar todos los botones en la carga y eso es todo. No puedo agregar acción en cada button, me refiero a labelrlos.

Es el primero en hacer el button de declarar de esta manera. Los botones no funcionan.

Aquí está mi código:

- (void)loadView { [super loadView]; self.view.backgroundColor = [UIColor grayColor]; mainHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"feat.png"]]; [mainHolder setUserInteractionEnabled:YES]; [mainHolder setFrame: CGRectMake(0, 0, 1024, 768)]; [self.view addSubview: mainHolder]; header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.png"]]; [header setFrame:CGRectMake(0, 0, 1024, 50)]; [header setUserInteractionEnabled:YES]; [mainHolder addSubview:header]; // The items to be displayed in the carousel items = [NSArray arrayWithObjects: [UIImage imageNamed:@"picOne.png"], [UIImage imageNamed:@"picTwo.png"], [UIImage imageNamed:@"picThree.png"], [UIImage imageNamed:@"picFour.png"], [UIImage imageNamed:@"picFive.png"], [UIImage imageNamed:@"picSix.png"], [UIImage imageNamed:@"picSeven.png"], [UIImage imageNamed:@"picEight.png"], [UIImage imageNamed:@"picNine.png"], [UIImage imageNamed:@"picTen.png"], [UIImage imageNamed:@"picEleven.png"], nil]; // Initialize and configure the carousel carousel = [[iCarousel alloc] initWithFrame:self.view.bounds]; carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; carousel.type = iCarouselTypeCoverFlow2; carousel.dataSource = self; [self.view addSubview:carousel]; } #pragma mark - #pragma mark iCarousel methods - (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel { return [items count]; } - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index { UIImage *image = [items objectAtIndex:index]; imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease]; [imageChoices setBackgroundImage:image forState:UIControlStateNormal]; [imageChoices setUserInteractionEnabled:YES]; [imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50]; [imageChoices addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; imageChoices.tag=index; return imageChoices; } - (void)buttonTapped:(UIButton *)sender { //I don't know how to tag each button here. switch(sender.tag){ case 1:{ NSLog(@"Photo1"); } break; case 2:{ NSLog(@"Photo2"); } case 3:{ NSLog(@"Photo3"); } break; case 4:{ NSLog(@"Photo4"); } break; case 5:{ NSLog(@"Photo5"); } break; } } 

Implementar el método de destino de esta manera:

 - (IBAction)buttonTapped:(id)sender { switch(sender.tag){ case 1:{ NSLog(@"Photo1"); break; } case 2:{ NSLog(@"Photo2"); break; } case 3:{ NSLog(@"Photo3"); break; } case 4:{ NSLog(@"Photo4"); break; } case 5:{ NSLog(@"Photo5"); break; } default:{ NSLog(@"Default"); break; } } } 
 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index { UIImage *image = [items objectAtIndex:index]; //Instead of adding single UIButton, add separate for each carousel. UIButton *imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease]; [imageChoices setBackgroundImage:image forState:UIControlStateNormal]; [imageChoices setUserInteractionEnabled:YES]; [imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50]; [imageChoices addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; imageChoices.tag=index; //print index, that it should be unique or not? return imageChoices; } 

Prueba esto

 - (void)buttonTapped:(UIButton *)sender { UIButton *sendButton = (UIButton *)sender; //No need of this! We already have sender switch(sendButton.tag) { //....... } } 

agregar carousel.delegate = self; En su método loadview y luego aplique

 - (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }