我是Objective-C的新手,有人可以帮我吗?我正在尝试制作按钮轮播,但我没有做到。
我得到它只是在加载时显示所有按钮,仅此而已。我不能在每个按钮上添加操作,我的意思是标记它们。
这是我第一次以这种方式声明按钮。按钮不起作用。
这是我的代码:
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
mainHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"feat.png"]];
[mainHolder setUserInteractionEnabled:YES];
[mainHolder setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview: mainHolder];
header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.png"]];
[header setFrame:CGRectMake(0, 0, 1024, 50)];
[header setUserInteractionEnabled:YES];
[mainHolder addSubview:header];
// The items to be displayed in the carousel
items = [NSArray arrayWithObjects:
[UIImage imageNamed:@"picOne.png"],
[UIImage imageNamed:@"picTwo.png"],
[UIImage imageNamed:@"picThree.png"],
[UIImage imageNamed:@"picFour.png"],
[UIImage imageNamed:@"picFive.png"],
[UIImage imageNamed:@"picSix.png"],
[UIImage imageNamed:@"picSeven.png"],
[UIImage imageNamed:@"picEight.png"],
[UIImage imageNamed:@"picNine.png"],
[UIImage imageNamed:@"picTen.png"],
[UIImage imageNamed:@"picEleven.png"],
nil];
// Initialize and configure the carousel
carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeCoverFlow2;
carousel.dataSource = self;
[self.view addSubview:carousel];
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [items count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index;
return imageChoices;
}
- (void)buttonTapped:(UIButton *)sender
{
//I don't know how to tag each button here.
switch(sender.tag){
case 1:{
NSLog(@"Photo1");
}
break;
case 2:{
NSLog(@"Photo2");
}
case 3:{
NSLog(@"Photo3");
}
break;
case 4:{
NSLog(@"Photo4");
}
break;
case 5:{
NSLog(@"Photo5");
}
break;
}
}