Domanda

// 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.
  ...
}

Perché non rileva che UIButton è nell'array?


Modifica

Si scopre che non lo rileverà nemmeno dopo che è stato aggiunto:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[[self hBtns] addObject:btn];
if ([[self hBtns] containsObject:btn]) // Returns false.
È stato utile?

Soluzione 2

Avevo dimenticato di inizializzare l'array (* doh *):

[self setHBtns:[[NSMutableArray alloc] initWithCapacity:0]];

Altri suggerimenti

Sembra che il confronto isEqual: non riesca. Puoi dare un'occhiata al hash per il pulsante UIB in entrambi i punti (dove viene aggiunto, quindi in didPushBtn ) e vedere se hanno lo stesso valore?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top