通过循环,我将UIImageView添加到UIScrollView中,我需要添加一个额外的参数addTarget,以便在单击时可以记录索引。

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


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


我该如何实现?

最佳答案

当您添加目标时,被调用的方法可以没有参数(例如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]);
}

关于iphone - cocoa UIImageView addTarget与额外的参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3990273/

10-12 01:49