accessing UIButton by (id)sender
Asked Answered
A

5

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

Ardellardella answered 6/11, 2010 at 13:28 Comment(0)
G
8

Send setHidden message to sender:

-(IBAction)ATapped:(id)sender{
   //want some way to hide the button which is tapped
   [sender setHidden:YES];
}
Gainer answered 6/11, 2010 at 13:31 Comment(5)
I tried sender.hidden = YES; before I posted this. it gave an error.Ardellardella
Ok, .hidden gives a "not a structure or union" error, but[ setHidden:YES] works ;)Ardellardella
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 setHiddenGainer
Or a check to see if it was a button. (if ([sender isMemberOfClass:[UIButton class]]){ [sender setHidden:YES]; })Chrissa
@Emil, better use isKindOfClass: then - to handle also UIButton subclass cases. Even better to use respondsToSelector: method for checkGainer
V
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];
}
Verditer answered 6/11, 2010 at 13:37 Comment(1)
Both methods can throw an error: my Method could throw a Casting Error and Vladimirs Method could throw a messaging errorUnconventional
G
8

Send setHidden message to sender:

-(IBAction)ATapped:(id)sender{
   //want some way to hide the button which is tapped
   [sender setHidden:YES];
}
Gainer answered 6/11, 2010 at 13:31 Comment(5)
I tried sender.hidden = YES; before I posted this. it gave an error.Ardellardella
Ok, .hidden gives a "not a structure or union" error, but[ setHidden:YES] works ;)Ardellardella
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 setHiddenGainer
Or a check to see if it was a button. (if ([sender isMemberOfClass:[UIButton class]]){ [sender setHidden:YES]; })Chrissa
@Emil, better use isKindOfClass: then - to handle also UIButton subclass cases. Even better to use respondsToSelector: method for checkGainer
U
2

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

-(IBAction)ATapped:(id)sender{
   // Possible Cast
   UIButton* myButton = (UIButton*)sender;
   myButton.hidden = YES;
}
Unconventional answered 6/11, 2010 at 13:31 Comment(0)
M
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;
   }
}
Meg answered 6/11, 2010 at 16:21 Comment(1)
I believe NSButton should be UIButton, but yes, thanks for this.Coss
E
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...

Excursus answered 18/12, 2010 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.