// Add the button to the NSMutableArray.
...
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[[self hBtns] addObject:btn];
...
// In another method, try to see if it exists.
- (void)didPushBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
if ([[self hBtns] containsObject:btn]) // Is false every time.
...
}
为什么没有检测到UIButton在数组中?
修改
事实证明,它在添加之后甚至都无法检测到它:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[[self hBtns] addObject:btn];
if ([[self hBtns] containsObject:btn]) // Returns false.
答案 0 :(得分:3)
听起来isEqual:
比较失败了。您是否可以在两个地方(添加它,然后在hash
中)查看UIButton的didPushBtn
,看看它们是否是相同的值?
答案 1 :(得分:3)
我忘了初始化数组(* doh *):
[self setHBtns:[[NSMutableArray alloc] initWithCapacity:0]];