2013-05-02 77 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 

} 
相關問題