0
// 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.
4

2 回答 2

3

听起来isEqual:比较失败。你能看看hash两个地方的 UIButton (它被添加的地方,然后在didPushBtn),看看它们是否是相同的值?

于 2009-08-05T21:32:31.640 回答
3

我忘记初始化数组(*doh*):

[self setHBtns:[[NSMutableArray alloc] initWithCapacity:0]];
于 2009-08-05T21:48:57.487 回答