Reputation: 2941
I have a view that contains a couple of subviews, each of these subviews have a transparent button on it. I place these views on my main UIView in a for-loop, in this loop I also assigns a target and an action for the subviews button property. My problem is that only the first view and button that gets created in the loop works (that is, it's action is getting called when I click it).
The relevant part of the loop looks like this:
// _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];
[...]
}
The action looks like this:
- (void)openCategoryWithId:(UIButton *)sender
{
// Only gets called when I click on the first views button.
NSLog(@"Hello");
}
I have tried to create a strong property for the subview, but that didn't help.
Any ideas on what I should do?
Upvotes: 0
Views: 406
Reputation: 1176
It looks like the buttons were not being positioned correctly because of categoryRect
.
Upvotes: 1