带有额外参数的cocoa UIImageView addTarget

时间:2010-10-21 17:35:36

标签: iphone cocoa-touch uiimageview

使用循环我将UIImageView添加到UIScrollView我需要添加一个额外的参数addTarget,所以当我点击我可以记录索引。

[imageButton addTarget:self action:@selector(buttonPushed:) 
      forControlEvents:UIControlEventTouchUpInside];


-(IBaction) buttonPushed: (int) index
{
    NSLog(@"%d",index);
}

我如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

当您添加目标时,正在调用的方法可以没有参数(例如buttonPushed)或者有一个(buttonPushed:),这是发送事件的控件(在本例中是您的按钮) 。如果需要索引或任何其他值,则需要在发送事件的按钮上进行设置。例如,当您设置按钮时:

myButtons = [NSArray arrayWithObjects:myFirstButton, mySecondButton, nil];
[myFirstButton addTarget:self action:@selector(buttonPushed:) 
  forControlEvents:UIControlEventTouchUpInside];
[mySecondButton addTarget:self action:@selector(buttonPushed:) 
  forControlEvents:UIControlEventTouchUpInside];

并将您的操作实施为

- (IBaction)buttonPushed:(UIButton *)button
{
    NSLog(@"%d",[myButtons indexOfObject:button]);
}

答案 1 :(得分:1)

使用按钮的tag属性

相关问题