gpt4 book ai didi

iphone - 如何识别在 Objective-C 中单击了哪个按钮?

转载 作者:可可西里 更新时间:2023-11-01 06:18:49 26 4
gpt4 key购买 nike

我是一名新的 iPad 开发者。

我以编程方式创建了 UIButton,我想在其中识别用户点击了哪个按钮,并据此执行一些操作。

我应该如何识别它?

这是我的代码片段:

for(int i=0;i<[_array count];i++)
{

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=count;
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];

button.backgroundColor=[UIColor redColor];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(xpos, 270.0, 200.0, 150.0);

[self.view addSubview:button];
}

这就是我现在正在做的事情:我想我会为每个按钮分配计数,然后将这个计数传递给按钮点击方法。这是一个好主意吗?还有其他办法吗?

单击按钮时,我正在调用 aMethod:

-(void)aMethod:(id)sender{
NSLog(@"btn clicked");

}

任何帮助将不胜感激!

最佳答案

-(void)aMethod:(UIButton*)sender{
NSLog(@"btn clicked %d", sender.tag);

}

关于iphone - 如何识别在 Objective-C 中单击了哪个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10612754/

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