如何为icarousel中的每个按钮创建单独的操作?

时间:2012-06-17 08:07:22

标签: iphone ios xcode ibaction icarousel

我创建了一个应用程序,使用icarousel.i在coverflow中显示8个按钮。必须为每个按钮分配不同的操作。这是我的viewcontroller.m文件的代码..

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

这里我已经卡住了...现在我想为每个按钮分配不同的动作。但是如果我在buttonTapped下声明一个动作,它将被分配给所有按钮。任何人都可以帮忙吗?...

除了我之前的问题,我已经在xib文件中添加了其他两个按钮和两个事件,并在我的.m文件中定义了方法...但是如果我在模拟器上运行它只是显示为图像并且无法进行交互用那个按钮......任何想法都会......

2 个答案:

答案 0 :(得分:1)

只需根据按钮标记值调用。

获取按钮标记值

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

uibutton *btn=(uibutton *) sender;

nslog(@"button tag== %d",btn.tag);

}

答案 1 :(得分:1)

您可以询问旋转木马按下哪个按钮:

- (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....
    }
}
相关问题