gpt4 book ai didi

iphone - cocoa UIImageView addTarget 带有额外参数

转载 作者:行者123 更新时间:2023-12-03 20:57:30 24 4
gpt4 key购买 nike

通过循环,我将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com