从多个 UIButton 调用一个选择器
我知道这里有一些问题与此非常相似,但我没有找到我需要的信息。我正在 for 循环中将 UIButtons 网格添加到我的视图中。每次创建新按钮时,我都会调用 addTarget:self action:@selector(doYourThing:) forControlEvent:UIControlEventTouchUpInside
。如果我只制作一个按钮,它就可以工作。但是,当我使用 for 循环创建整个按钮网格时,没有一个按钮想要调用选择器,而是得到 EXC_BAD_ACCESS。有什么建议吗?谢谢大家。
- (void)viewDidLoad
{
[super viewDidLoad];
[titleLabel setText:[site title]];
// Do any additional setup after loading the view from its nib.
buttonArray = [[NSMutableArray alloc] initWithCapacity:250];
int i = 0;
int c = 0;
int index = 0;
for (c=0; c < [site columnCount]; c++) {
for (i = 0; i < [site rowCount]; i++) {
plotButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[plotButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
CGPoint origin = CGPointMake(60.0, 40.0);
[plotButton setTitle:@"Plot" forState:UIControlStateNormal];
[plotButton setFrame:CGRectMake(origin.x + (90.0 * c), origin.y + (45.0 * i), 90.0, 45.0)];
[plotButton setTag:index];
[buttonArray insertObject:plotButton atIndex:index];
NSLog(@"%d", [plotButton tag]);
index ++;
[[self view] addSubview:plotButton];
}
}
}
这是我的 for 循环。我不知道这样嵌套它们是否很愚蠢,但我正在制作一个按钮网格,这似乎是完成行和列的明智方法。无论如何,谢谢。
添加异常断点。得到:捕获点 2(抛出)挂起的断点 1 - “objc_exception_throw”已解决。
I know there are some questions on here that are very much like this but I am not finding the info I need. I am adding a grid of UIButtons to my view in a for loop. Each time a new button is created, I call addTarget:self action:@selector(doYourThing:) forControlEvent:UIControlEventTouchUpInside
. If I only make one button, it works. But when I create a whole grid of buttons using the for loop, none of the buttons want to call the selector and instead I get EXC_BAD_ACCESS. Any suggestions? Thanks everyone.
- (void)viewDidLoad
{
[super viewDidLoad];
[titleLabel setText:[site title]];
// Do any additional setup after loading the view from its nib.
buttonArray = [[NSMutableArray alloc] initWithCapacity:250];
int i = 0;
int c = 0;
int index = 0;
for (c=0; c < [site columnCount]; c++) {
for (i = 0; i < [site rowCount]; i++) {
plotButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[plotButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
CGPoint origin = CGPointMake(60.0, 40.0);
[plotButton setTitle:@"Plot" forState:UIControlStateNormal];
[plotButton setFrame:CGRectMake(origin.x + (90.0 * c), origin.y + (45.0 * i), 90.0, 45.0)];
[plotButton setTag:index];
[buttonArray insertObject:plotButton atIndex:index];
NSLog(@"%d", [plotButton tag]);
index ++;
[[self view] addSubview:plotButton];
}
}
}
Here's my for loop. idk if it's dumb to nest them like that but I'm making a grid of buttons and that seemed the sensible way to get rows and columns done. Anyways, thanks.
Added exception breakpoint. Got: Catchpoint 2 (throw)Pending breakpoint 1 - "objc_exception_throw" resolved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我用整数替换 [site columnCount] 和 [site rowCount] 时,您的代码工作正常。我的猜测是您在这两种方法之一中遇到了问题。
Your code works fine when I replace [site columnCount] and [site rowCount] by an integer. My guess is that you have a problem in one of those two methods.
我猜你调用了 [UIButon buttonWithType:...] 构造函数,然后你确实释放了按钮...
但我只是猜测。为了确定起见,您应该在此处编写代码。
再见!
I guess you called the [UIButon buttonWithType:...] constructor and then you did release the button...
But I'm just guessing. You should write the code here in order to be sure.
See you!