Check which UIButton was Clicked from another function - ios

In my xib I have taken 4 UIbuttons named button 1, 2, 3 and 4. These four buttons are connected two four different IBAction methods which perform different functions.
Now I have one more button called "Save" This has also a different IBAction method.
- (IBAction)Save:(id)sender
{
}
Now here I want to check which of the above 4 UIButtons have been clicked.
For this I tried checking this way
- (IBAction)Save:(id)sender
{
if(sender == button1)
{
//Do this
}
else if (sender == button2)
{
//Do this
}
}
But this is not working. I am doing something wrong.Please help me out
Regards
Ranjit.

You can set the tag values for each button in the interface builder and set actions of all buttons to this method
//set global variable flag.
int flag;
- (IBAction)buttonClicked:(id)sender
{
switch ([sender tag])
{
case 0:
{
flag =0;
// implement action for first button
}
break;
case 1:
{
flag =1;
// implement action for second button
}
break;
case 2:
{
flag =2;
// implement action for third button
}
break;
//so on
default:
break;
}
}
for save button
- (IBAction)save:(id)sender
{
switch (flag)
{
case 0:
{
// first button clicked
}
break;
case 1:
{
// second button clicked
}
break;
case 2:
{
// third button clicked
}
break;
//so on
default:
break;
}
}

Define a class level ivar as
UIButton *selectedBtn;
Then in you IBActions
- (IBAction)button1:(id)sender {
selectedBtn = sender // or button1
}
- (IBAction)button2:(id)sender {
selectedBtn = sender // or button2
}
- (IBAction)button3:(id)sender {
selectedBtn = sender // or button3
}
- (IBAction)button4:(id)sender {
selectedBtn = sender // or button4
}
- (IBAction)Save:(id)sender
{
//Check output of below statement to ensure you're getting a sender
NSLog(#"Sender: %#", sender);
if(selectedBtn == button1)
{
NSLog(#"Button 1 pressed");
//Do this
}
else if (selectedBtn == button2)
{
NSLog(#"Button 2 pressed");
//Do this
}
else if (selectedBtn == button3)
{
NSLog(#"Button 3 pressed");
//Do this
}
else if (selectedBtn == button4)
{
NSLog(#"Button 4 pressed");
//Do this
}
}

Can you try this:
- (IBAction)Save:(id)sender
{
UIButton *pressedButton = (UIButton*)sender;
//Check output of below statement to ensure you're getting a sender
NSLog(#"Sender: %#", sender);
if([pressedButton isEqual:button1])
{
NSLog(#"Button 1 pressed");
//Do this
}
else if ([pressedButton isEqual:button2])
{
NSLog(#"Button 2 pressed");
//Do this
}
}

In your save method, check the Selected property of the other 4 buttons. If you do not want to keep the buttons in a selected state, but just want to see if they were clicked at some point, then define a property (e.g. an array) to track which buttons were clicked during the session, and check this property in your save method.

Related

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 use sender attributes in Objective-C

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.

How to check if my button is selected?

If my button is in the "selected" state, I want an activity indicator to appear to let the user know that something is happening and that my app is searching for data. I've tried to accomplish this FIRST by using NSLog to see if I can get this method working FIRST. So I did this:
- (void)viewDidLoad
{
[super viewDidLoad];
searchedItem.delegate = self;
if(searchButton.selected == YES)
{
NSLog(#"The button was selected");
}
}
For some reason, it won't work.
Put this checkmark in your button click event. It perfect work when click on button as first time then got to Not Selected and after click on second time go to Selected.
-(IBAction)ClickBtn:(UIButton *)sender
{
sender.selected = ! sender.selected;
if (sender.selected)
{
NSLog(#" Not Selected");
}
else
{
NSLog(#" Selected");
}
}
try this
- (void)viewDidLoad
{
[super viewDidLoad];
searchedItem.delegate = self;
[searchButton setSelected:YES];
if(searchButton.selected == YES)
{
NSLog(#"The button was selected");
}
}

Combining different UIButton taps together

I'm trying to make an app where the user has 4 buttons let's say labeled 1 through 4. If the user taps button-1 then something happens ( I know how to do this part). But let's say I want something new to happen if the user taps button 1 and then button 2. Is there a way to combine taps if so I would greatly appreciate the help.
For this you can use [btn1 isSelected] method ie. you can check if btn1 is selected and then btn 2 is pressed, then perform your desired action.
What you have to do is just select and deselect your buttons when they are tapped and check for their state on other button's action.
- (IBAction)btn1Action:(id)sender {
UIButton *btn1 = (UIButton *)sender;
if ([btn1 isSelected]) {
[btn1 setSelected:NO];
}
else{
[btn1 setSelected:NO];
}
}
- (IBAction)btn2Action:(id)sender {
// Button 1 outlet is myBtn1 (Suppose)
UIButton *btn1 = (UIButton *)sender;
if ([myBtn1 isSelected]) {
// Pressed after button 1
}
else{
// Pressed after button 2
}
}
The code Snippet shared below, gives you an understanding as to how to trigger events for multiple button click combination.
In this case, i have used only a button combination of 2.And I'm disabling the selected buttons once the event is over.
-(IBAction)clickButton:(UIButton *)sender
{
switch (sender.tag) {
case 0:
{
if(_button2.isSelected)
{
//do task for combination of 2,1
_button2.selected=NO;
_button1.selected=NO;
}
if (_button3.isSelected)
{
//do task for combination of 3,1
_button3.selected=NO;
_button1.selected=NO;
}
if(_button4.isSelected)
{
//do task for combination of 4,1
_button4.selected=NO;
_button1.selected=NO;
}
else
{
//do task for button1 alone
_button1.selected=NO;
}
}
break;
case 1:
{
if(_button1.isSelected)
{
//do task for combination of 1,2
_button1.selected=NO;
_button2.selected=NO;
}
if (_button3.isSelected)
{
//do task for combination of 3,2
_button3.selected=NO;
_button2.selected=NO;
}
if (_button4.isSelected)
{
//do task for combination of 4,2
_button4.selected=NO;
_button2.selected=NO;
}
else
{
//do task for button2 alone
_button2.selected=NO;
}
}
case 2:
{
if(_button1.isSelected)
{
//do task for combination of 1,3
_button1.selected=NO;
_button3.selected=NO;
}
if (_button2.isSelected)
{
//do task for combination of 2,3
_button2.selected=NO;
_button3.selected=NO;
}
if (_button4.isSelected)
{
//do task for combination of 4,3
_button4.selected=NO;
_button3.selected=NO;
}
else
{
//do task for button3 alone
_button3.selected=NO;
}
}
case 3:
{
if(_button1.isSelected)
{
//do task for combination of 1,4
_button1.selected=NO;
_button4.selected=NO;
}
if (_button2.isSelected)
{
//do task for combination of 2,4
_button2.selected=NO;
_button4.selected=NO;
}
if (_button3.isSelected)
{
//do task for combination of 3,4
_button3.selected=NO;
_button4.selected=NO;
}
else
{
//do task for button4 alone
_button4.selected=NO;
}
}
case 4:
{
_button1.selected=NO;
_button2.selected=NO;
_button3.selected=NO;
_button4.selected=NO;
}
default:
break;
}
}
Hope it Helps !!!

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