3

我是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;   
}
}
4

4 回答 4

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;
        }
    }
}
于 2012-09-27T07:18:45.507 回答
0

试试这个

- (void)buttonTapped:(UIButton *)sender
{

    UIButton *sendButton = (UIButton *)sender; //No need of this! We already have sender

    switch(sendButton.tag)
    {

        //.......

    }

}
于 2012-09-27T06:55:01.197 回答
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;
}
于 2012-09-27T06:55:01.727 回答
0

添加carousel.delegate = self;在您的 loadview 方法中,然后实施

- (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }
于 2012-10-09T12:09:26.293 回答