我是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;
}
}
答案 0 :(得分:2)
以这种方式实现目标方法:
- (IBAction)buttonTapped:(id)sender
{
switch(sender.tag){
case 1:{
NSLog(@"Photo1");
break;
}
case 2:{
NSLog(@"Photo2");
break;
}
case 3:{
NSLog(@"Photo3");
break;
}
case 4:{
NSLog(@"Photo4");
break;
}
case 5:{
NSLog(@"Photo5");
break;
}
default:{
NSLog(@"Default");
break;
}
}
}
答案 1 :(得分:0)
试试这个
- (void)buttonTapped:(UIButton *)sender
{
UIButton *sendButton = (UIButton *)sender; //No need of this! We already have sender
switch(sendButton.tag)
{
//.......
}
}
答案 2 :(得分:0)
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
//Instead of adding single UIButton, add separate for each carousel.
UIButton *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; //print index, that it should be unique or not?
return imageChoices;
}
答案 3 :(得分:0)
在你的loadview方法中添加carousel.delegate = self;
,然后实现
- (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }