从多个 UIButton 调用一个选择器

发布于 2025-01-02 11:44:41 字数 1427 浏览 1 评论 0原文

我知道这里有一些问题与此非常相似,但我没有找到我需要的信息。我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡寂寞咖啡 2025-01-09 11:44:41

当我用整数替换 [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.

揪着可爱 2025-01-09 11:44:41

我猜你调用了 [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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文