Search code examples
iphoneiosxcodeibactionicarousel

How to create individual action for each button in icarousel?


I've created an app that shows 8 buttons in coverflow using icarousel.i have to assign different action for each button.Here is my piece of code for viewcontroller.m file..

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return NUMBER_OF_ITEMS;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index   reusingView:(UIView *)view
{

UIImage *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                      [UIImage imageNamed:@"Cover_1.png"],
                      [UIImage imageNamed:@"Cover_2.png"],
                      [UIImage imageNamed:@"Cover_3.png"],
                      [UIImage imageNamed:@"Cover_4.png"],
                      [UIImage imageNamed:@"Cover_5.png"],
                      [UIImage imageNamed:@"Cover_6.png"],
                      [UIImage imageNamed:@"Cover_7.png"],nil];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);

[button setImage:(UIImage*)[buttonImage objectAtIndex:index] forState:UIControlStateNormal];    
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;   
}

- (void)buttonTapped:(UIButton *)sender
{
}

Here i've got stuck...now i want to assign different action for each button.but if i declare an action under buttonTapped it gets assigned for all the buttons..Can anyone help?...

additional to my previous question i've added other two buttons and two event to that buttons in xib file and defined method in my .m file...But if i run that on simulator its just displayed as image and cant interact with that button...Any idea pls...


Solution

  • You can ask the carousel which button was pressed like this:

    - (void)buttonTapped:(UIButton *)sender
    {
        NSInteger index = [carousel indexOfItemViewOrSubview:sender];
        switch(index) {
            case 0:
                //do action number 1
                break;
            case 1:
                //do action number 2
                break;
            etc....
        }
    }