2

icarouselを使用してcoverflowで8つのボタンを表示するアプリを作成しました。ボタンごとに異なるアクションを割り当てる必要があります。これが、viewcontroller.mファイルのコードです。

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return NUMBER_OF_ITEMS;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index   reusingView:(UIView *)view
{

UIImage *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                      [UIImage imageNamed:@"Cover_1.png"],
                      [UIImage imageNamed:@"Cover_2.png"],
                      [UIImage imageNamed:@"Cover_3.png"],
                      [UIImage imageNamed:@"Cover_4.png"],
                      [UIImage imageNamed:@"Cover_5.png"],
                      [UIImage imageNamed:@"Cover_6.png"],
                      [UIImage imageNamed:@"Cover_7.png"],nil];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);

[button setImage:(UIImage*)[buttonImage objectAtIndex:index] forState:UIControlStateNormal];    
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;   
}

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

ここで行き詰まりました...ボタンごとに異なるアクションを割り当てたいのですが、buttonTappedでアクションを宣言すると、すべてのボタンに割り当てられます..誰か助けてもらえますか?...

前の質問に加えて、xibファイルのそのボタンに他の2つのボタンと2つのイベントを追加し、.mファイルのメソッドを定義しました...しかし、シミュレーターでそれを実行すると、画像として表示され、そのボタンと対話できません...任意のアイデアpls..。

4

2 に答える 2

1

ボタンタグの値に基づいてベースを呼び出すだけです。

ボタンタグ値を取得

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

uibutton *btn=(uibutton *) sender;

nslog(@"button tag== %d",btn.tag);

}
于 2012-06-17T08:13:57.280 に答える
1

次のように、どのボタンが押されたかをカルーセルに尋ねることができます。

- (void)buttonTapped:(UIButton *)sender
{
    NSInteger index = [carousel indexOfItemViewOrSubview:sender];
    switch(index) {
        case 0:
            //do action number 1
            break;
        case 1:
            //do action number 2
            break;
        etc....
    }
}
于 2012-06-17T08:21:12.237 に答える