【问题标题】:How to create individual action for each button in icarousel?如何为 icarousel 中的每个按钮创建单独的操作?
【发布时间】:2012-06-17 08:07:21
【问题描述】:

我创建了一个应用程序,它使用 icarousel 在 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 文件中定义了方法...但是如果我在模拟器上运行它,它只会显示为图像并且无法交互用那个按钮...任何想法请...

【问题讨论】:

    标签: iphone ios xcode ibaction icarousel


    【解决方案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....
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      获取按钮标签值

      - (void)buttonTapped:(UIButton *)sender
      {
      
      uibutton *btn=(uibutton *) sender;
      
      nslog(@"button tag== %d",btn.tag);
      
      }
      

      【讨论】:

      • tks...但它似乎不适用于我的代码..这是我们可以使用 NSArray 声明操作的一种方式吗?...
      • 嘿,首先在你的代码中添加按钮标签 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view { button.tag=index;跨度>
      • 这曾经是推荐的解决方案,但 iCarousel 现在有一个内置方法可以轻松完成此操作,而无需使用标签。见我上面的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2013-09-13
      • 1970-01-01
      相关资源
      最近更新 更多