2013-05-02 75 views
1

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

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

为selection.but如何为这两个按钮在两个具体行动整体看法?

在此先感谢。

+0

试试这个它为我工作。 [试试这个](http://stackoverflow.com/questions/22145978/my-custom-button-is-not-getting-click-in-icarousel-ios) – vijay 2016-02-26 15:32:12

回答

1

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

如果您使用笔尖创建视图,请参阅iCarousel示例项目中包含的控件示例以了解如何执行此操作。相应的,事件

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

同样,

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

1

你为什么不试试这个

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

} 
相关问题