How to use sender attributes in Objective-C - ios

I'm using two buttons to do the same action. How can I identify which button was pressed using the "sender" from my IBAction function? (each button has a different Tag)
Here is what I want to do:
- (IBAction)addItemInParent:(id)sender {
NSInteger choice = sender.tag;
}
I hope this question makes sense!

- (IBAction)addItemInParent:(id)sender
{
UIButton *buttonPressed = (UIButton *)sender;
if (buttonPressed.tag == 123)
{
//button with tag 123 was pressed
}else if (buttonPressed.tag == 124
{
//button with tag 124 was pressed
}
}
Just replace my 123 and 124 with what the button tags actually are.

If you are confident that this method is only invoked by a button press, then you can rewrite your method to this one:
- (IBAction)addItemInParent:(UIButton *)sender {
NSInteger choice = sender.tag;
}
If not, check for the class of the sender:
- (IBAction)addItemInParent:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)sender;
NSInteger choice = button.tag;
}
else {
// handle other cases
}
}

Change id to UIButton like so:
- (IBAction)addItemInParent:(UIButton *)sender {
NSInteger choice = sender.tag;
}

After casting sender to UIButton, did you try:
if (choice == 101)
{
// button whatever was pressed
}
else
{
// the other button was pressed
}
Obviously you should use the tags you assigned to the buttons.

You need to
1) check if the id object is a actually UIView class or subclass
2) typecast the id object to UIView
3) check the tag
4) decide what next based on the tag
if ([sender isKindOfClass:([UIView class])])
{
NSInteger tag = ((UIView *)sender).tag;
if (tag == <tag of button 1>)
{
//do something
}
else if (tag == <tag of button 2>)
{
//do something else
}
}
You need to typecast because id can be any type of object and you must make sure that it is actually an instance of UIView class or subclass to be able to access the tag, as only UIView classes and subclasses have a tag property.

Related

How do I use the sender argument to figure out what UIImageView was selected?

I have eight UIImageViews that I want to fade if the UITapGestureRecognizer that is associated with it is activated. I have the all recognizers hooked up to this IBAction:
- (IBAction)disableDie:(id)sender {
NSLog(#"%#", sender);
NSLog(#"%ld",[(UIGestureRecognizer *)sender view].tag);
}
I thought I could do it with a loop like this:
- (IBAction)disableDie:(id)sender {
for (UIImageView *numberImage in self.diceOutletArray) {
if (numberImage == sender) {
numberImage.alpha = 0.65;
}
}
NSLog(#"%#", sender);
NSLog(#"%ld",[(UIGestureRecognizer *)sender view].tag);
}
But nothing happens to the UIImageView that was pressed, but the message's are printed. I have used the diceOutletArray in other loops and it works.
The sender is a UITapGestureRecognizer, not a UIImageView, and
therefore numberImage == sender will never be true.
Try this instead:
- (IBAction)disableDie:(UIGestureRecognizer *)sender {
for (UIImageView *numberImage in self.diceOutletArray) {
if (numberImage == sender.view) {
numberImage.alpha = 0.65;
break;
}
}
}
You don't actually need the loop at all though, this would work fine as well:
- (IBAction)disableDie:(UIGestureRecognizer *)sender {
sender.view.alpha = 0.65;
}
The gesture recognizer is the sender, not the view. You should see that in the printout of sender. You need to get the recognizer's view (assuming that it's attached directly to its image view).
Once you have that, you don't really need to go and find another pointer to the view: you already have it. It's just called sender.view instead of mumbleMumbleImageView.
Just send setAlpha: to that pointer.

change BarButton item identifier on code

I have a bar button item on StoryBoard,its identifier is Play,
How can I change this bar button's identifier on code?
- (IBAction)play:(UIBarButtonItem *)sender {
// For example,when I touch this button,
// it change its Identifier to Stop
}
use UIButton's tag features
using UIButton outlet set tag for play like testButton.tag = 1;
then in
- (IBAction)play:(UIBarButtonItem *)sender {
if (sender.tag == 1) {
testButton.tag = 2;
} else if (sender.tag == 2) {
testButton.tag = 1;
}
}

How to get the object that called the method in iOS?

I have a button that calls a method. I need to access that button inside the method. I don't know how you can access the caller of a method.
-(void)myButtonAction: (id)sender
{
if ([sender isKindOfClass: [UIButton class]])
{
UIButton* btn = sender;
// do something! :)
}
}
if you're using IBAction for handling button event, the sender passed to you will be your button:
- (void) onButtonAction: (id) sender
{
NSLog(#"sender object description %#", sender);
}

Getting Button Tag Value in iPhone

I have made 20 Buttons dynamically, and I got the tag values of all Buttons.
But I need to know how to use that tag values.
I need information on every button pressed with tag values.
So, how do I use those tag values?
You need to set target-action of each button.
[button setTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
Then implement someMethod: like this:
- (void)someMethod:(UIButton *)sender {
if (sender.tag == 1) {
// do action for button with tag 1
} else if (sender.tag == 2) {
// do action for button with tag 2
} // ... and so on
}
Why do you need to use the tag to get the button. You can directly get the buttons reference from its action method.
- (void)onButtonPressed:(UIButton *)button {
// "button" is the button which is pressed
NSLog(#"Pressed Button: %#", button);
// You can still get the tag
int tag = button.tag;
}
I hope you have added the target-action for the button.
[button addTarget:self action:#selector(onButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
You can get reference to that your buttons using that tags. For example, you've added UIButtons to UIView *mainView. To get reference to that buttons you should write following:
UIButton *buttonWithTag1 = (UIButton *)[mainView viewWithTag:1];
Set the tags like this :
for (createButtonIndex=0; createButtonIndex<buttonsCount; createButtonIndex++)
{
buttonCaps.tag=createButtonIndex;
}
And add the method to trap the tags :-
-(void)buttonsAction:(id)sender
{
UIButton *instanceButton = (UIButton*)sender;
switch(instanceButton.tag)
{
case 1(yourTags):
//Code
break;
case 2:
//Code
break;
}
}
Hope this Helps !!
- (IBAction)buttonPressed:(id)sender {
UIButton selectedButton = (UIButton *)sender;
NSLog(#"Selected button tag is %d%", selectedButton.tag);
}
usefully we use btn tag if You Write One Function For (more than one) Buttons .in action if we want to write separate Action For button at that situvation we use btn tag.it can get two ways
I) case sender.tag
//if we have four buttons Add,mul,sub,div having Same selector and add.tag=10
mul.tag=20,sub.tag=30,div.tag=40;
-(IBAction) dosomthing:(id)sender
{
int x=10;
int y=20;
int result;
if(sender.tag==10)
{
result=x+y;
}else if(sender.tag==20)
{
result=x*y;
}else if(sender.tag==30)
{
result=x-y;
}else if(sender.tag==40)
{
result=x/y;
}
NSLog(#"%i",result);
}
2)Case
UIButton *btn=[self.view viewWithTag:10];
then you got object of add button uyou can Hide It With btn.hidden=YES;
UIButton *btn = (UIButton *)[mainView viewWithTag:button.tag];

Detect which button is pressed with an UIButton Array

I have an UIButton array like this:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btn_Impact_Collection;
and I have this function:
- (IBAction)impactAction:(id)sender;
In the XIB file I have nine button, each button is connected to btn_Impact_Collection Array with the Referencing Outlet Collection. Moreover the Touch_inside property of each button is connected to the function ImpactAction.
Now, when a button is clicked the ImpactAction function is called, but inside this function, how can i know which button is pressed?
Thanks in advance for the answer!
Cast sender to UIButton class, and that will give you the instance of the clicked button. I don't have Xcode with me but something like:
if ([sender isMemberOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)sender;
// Then you can reference the title or a tag of the clicked button to do some further conditional logic if you want.
if([btn.currentTitle isEqualToString:#"title of button"])
{
// do something.
}
else if(etc...)
}
Set tags for each button in interface builder (1-9), then say
if ([sender tag] == 1) {
//First button was pressed, react.
}
else if ([sender tag] == 2) {
//Second button was pressed, react.
}
// Etc...
else {
//Last button was pressed, react.
}
And the same for all the others, or you could put it in a switch.
Rather than do a string check on the title (which is slow and can be tedious) you can:
- (void)buttonPressed:(id)sender
{
for( UIButton *button in self.buttonCollection )
{
if( sender == button )
{
// sender is your button (eg. you can access its tag)
}
}
}
Another option.. Cast sender to check if it's a UIButton, then switch sender.tag:
if ([sender isMemberOfClass:[UIButton class]]) {
switch ([sender tag]) {
case 0:
//do stuff for button with tag 0
break;
case 1:
//do stuff for button with tag 1
break;
case 2:
....
break;
default:
break;
}
}

Resources