我将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");
}
用于整个视图选择。但如何为这两个按钮设置两个特定的操作?
提前致谢。
答案 0 :(得分:1)
在创建项目视图时将按钮操作绑定到视图控制器,然后在操作方法中使用indexForViewOrSubview:carousel方法来确定按下了哪个按钮。
如果您使用nib创建视图,请查看iCarousel示例项目中包含的控件示例,了解如何执行此操作。
答案 1 :(得分: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
}