gpt4 book ai didi

objective-c - iOS 添加目标 :action:forControlEvents only works for one of my button when assigned in for loop

转载 作者:行者123 更新时间:2023-11-29 04:33:50 26 4
gpt4 key购买 nike

我有一个包含几个 subview 的 View ,每个 subview 上都有一个透明按钮。我将这些 View 放在 for 循环中的主 UIView 上,在此循环中,我还为 subview 按钮属性分配一个目标和一个操作。我的问题是,只有在循环中创建的第一个 View 和按钮才有效(也就是说,当我单击它时,它的操作就会被调用)。

循环的相关部分如下所示:

// _categories is an array
for (int i = 0; i < _categories.count; i++) {

[...]

Category *category = [_categories objectAtIndex:i];
CategoryView *categoryView = [[CategoryView alloc] initWithFrame:categoryRect andTitle:category.name];
[...]

categoryView.categoryButton.tag = [category.categoryId intValue];
[categoryView.categoryButton addTarget:self action:@selector(openCategoryWithId:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:categoryView];

[...]
}

该操作如下所示:

- (void)openCategoryWithId:(UIButton *)sender
{
// Only gets called when I click on the first views button.
NSLog(@"Hello");
}

我尝试为 subview 创建一个强大的属性,但这没有帮助。

关于我应该做什么有什么想法吗?

最佳答案

由于 categoryRect 的原因,按钮的位置似乎不正确。

关于objective-c - iOS 添加目标 :action:forControlEvents only works for one of my button when assigned in for loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329688/

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