Search code examples
objective-ciosuibuttonicarousel

Objective C : Adding Action On Array of Buttons


i'm a newbie in Objective-C can someone help me out here? I am trying to make a carousel of button but i failed to do it.

What i got it just displaying all the buttons on load and that's it. I can't add action on each button, I mean tagging them.

It's my first to do declare button in this way. The buttons are not working.

Here's my code:

- (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;   
}
}

Solution

  • Implement the target method this way:

    - (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;
            }
        }
    }