5

I have the following code

-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
self.hidden = YES;
}

Which is linked to multiple buttons. I want to hide the button which triggered this IBAction. self.hidden is obviously not the button.

How do I hide the button which was tapped? The sender.

Thanks

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
some_id
  • 29,466
  • 62
  • 182
  • 304

5 Answers5

13

Both Vladimir and Henrik's answers would be correct. Don't let the 'id' type scare you. It's still your button object it's just that the compiler doesn't know what the type is. As such you can't reference properties on it unless it is cast to a specific type (Henrik's answer).

-(IBAction)ATapped:(id)sender{
   // Possible Cast
   UIButton* myButton = (UIButton*)sender;
   myButton.hidden = YES;
}

Or you can send any message (call any method) on the object, assuming YOU know the type (which you do, it's a button), without having to cast (Vladimir's answer).

-(IBAction)ATapped:(id)sender{
   //want some way to hide the button which is tapped
   [sender setHidden:YES];
}
shanezilla
  • 457
  • 2
  • 8
8

Send setHidden message to sender:

-(IBAction)ATapped:(id)sender{
   //want some way to hide the button which is tapped
   [sender setHidden:YES];
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • I tried sender.hidden = YES; before I posted this. it gave an error. – some_id Nov 06 '10 at 13:46
  • Ok, .hidden gives a "not a structure or union" error, but[ setHidden:YES] works ;) – some_id Nov 06 '10 at 13:52
  • To user property .hidden you must case sender to UIButton - like in Henrik answer, and setHidden message you can send to any object. However if you're not 100% sure that sender is UIButton then you should perform a check if sender responds to setHidden – Vladimir Nov 06 '10 at 13:59
  • Or a check to see if it was a button. (`if ([sender isMemberOfClass:[UIButton class]]){ [sender setHidden:YES]; }`) – Emil Nov 06 '10 at 16:39
  • 1
    @Emil, better use isKindOfClass: then - to handle also UIButton subclass cases. Even better to use respondsToSelector: method for check – Vladimir Nov 06 '10 at 16:56
2

Your getting the button object (id) provided as a parameter

-(IBAction)ATapped:(id)sender{
   // Possible Cast
   UIButton* myButton = (UIButton*)sender;
   myButton.hidden = YES;
}
Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
2

If you want bullet proof cast/messaging, try this:

-(IBAction)ATapped:(id)sender{
   // Secure Cast of sender to UIButton
   if ([sender isKindOfClass:[UIButton class]]) {
       UIButton* myButton = (UIButton*)sender;
       myButton.hidden = YES;
   }
}
Florian
  • 5,918
  • 3
  • 47
  • 86
Erdemus
  • 2,728
  • 2
  • 17
  • 10
0

And... if you want to change the backgroundcolor of a button, the correct code will be like this?

[sender setBackgroundColor:(NSColor *)redColor];

for example? ... because it is´nt works for my...

MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
AlextaNET
  • 55
  • 1
  • 1
  • 7