1

我将 iCarousel 集成到我的应用程序中。它工作正常。但我的要求是在单个视图中显示两个按钮以及这两个按钮的特定操作。我将按钮显示为

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

 UIView *sampleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 300)];
    sampleView.backgroundColor=[UIColor whiteColor];

UIButton*  btntrans=[UIButton buttonWithType:UIButtonTypeCustom];
    [btntrans setFrame:CGRectMake(45, 40, 105, 50)];

    [btntrans setBackgroundColor:[UIColor clearColor]];
    btntrans.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    [btntrans setTitle:@"" forState:UIControlStateNormal];
    [btntrans setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sampleView addSubview:btntrans]; 
UIButton*  btntrans1=[UIButton buttonWithType:UIButtonTypeCustom];
    [btntrans1 setFrame:CGRectMake(45, 90, 105, 50)];

    [btntrans1 setBackgroundColor:[UIColor clearColor]];
    btntrans1.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    [btntrans1 setTitle:@"" forState:UIControlStateNormal];
    [btntrans1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sampleView addSubview:btntrans1]; 
return sampleView;
}

我们可以用

-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"ITEM SELECTED");
}

对于整个视图选择。但是如何为这两个按钮设置两个特定的操作?

提前致谢。

4

2 回答 2

1

创建项目视图时将按钮操作绑定到视图控制器,然后在操作方法中使用轮播的 indexForViewOrSubview: 方法来确定按下了哪个按钮。

如果您使用 nib 创建视图,请查看 iCarousel 示例项目中包含的控件示例,了解如何执行此操作。

于 2013-05-05T09:38:47.030 回答
1

你为什么不试试这个:

[btntrans addTarget:self
             action:@selector(first_action)
   forControlEvents:UIControlEventTouchUpInside];

相似地,

[btntrans1 addTarget:self
              action:@selector(second_action)
    forControlEvents:UIControlEventTouchUpInside];

相应地在事件中:

- (void)first_action:(id)sender{

    //This will get you the index of the selected view

    NSInteger index = [self.carousel indexOfItemViewOrSubview:sender];
                 .......
    //Do whatever you feel like

}
于 2013-06-19T13:06:54.727 回答