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;
}
}
Related
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.
I have 5 buttons in my xib, Button 1 to 4 is mapped to
-(IBAction)btn1to4:(UIButton *)sender;
Button 5 is mapped to
-(IBAction)btnFive:(id)sender;
Initially all the 4 buttons are hidden and only button 5 is visible, What I need is when I click on Button 5 all the 4 buttons should appear and when I click again on Button 5 they should disappear. For individual button I can write code in Button 5 as button1.hidden=NO, button2.hidden=NO and soon. But my buttons 1 to 4 are mapped to single btn1to4 method. How should I code in my btnFive method to hide/Unhide all 4 buttons at once?
Add buttons 1 through 4 to an IBOutletCollection in the interface builder. Add a property
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons1_4;
and drag buttons 1..4 there. Here is an answer explaining how to do it step-by-step.
Now you can operate all the buttons in that collection using a loop, rather than referencing them individually:
-(void)flipButtonsVisibility:(UIButton*)sender {
for (UIButton *btn in buttons1_4) {
btn.hidden = !btn.hidden;
}
}
Give tag of your button5 such like
button5.tag = 101;
In you button5's IBAction change id to UIButton * in parameter, such like
-(IBAction)btnFive:(UIButton *)sender
And Write following code
-(IBAction)btnFive:(UIButton *)sender
{
if(sender.tag == 101)
{
self.btn1.hidden = YES;
self.btn2.hidden = YES;
self.btn3.hidden = YES;
self.btn4.hidden = YES;
sender.tag = 102;
}
else
{
self.btn1.hidden = NO;
self.btn2.hidden = NO;
self.btn3.hidden = NO;
self.btn4.hidden = NO;
sender.tag = 101;
}
}
in your .h file
int a;
.m
-(void)viewDidLoad
{
a=0;
}
In your button click
-(IBAction)btnFive:(id)sender
{
if(a==0)
{
button1.hidden = YES;
button2.hidden = YES;
button3.hidden = YES;
button4.hidden = YES;
a = 1;
}
else
{
button1.hidden = NO;
button2.hidden = NO;
button3.hidden = NO;
button4.hidden = NO;
a = 0;
}
}
In your -(IBAction)btnFive:(id)sender, first check any one button (from 1-4) hidden property & do its opposite in condition. please find sample below -
-(IBAction)btnFive:(id)sender {
if(btn4.hidden==false){
btn1.hidden=true;
btn2.hidden=true;
btn3.hidden=true;
btn5.hidden=true;
}else{
btn1.hidden=false;
btn2.hidden=false;
btn3.hidden=false;
btn5.hidden=false;
}
}
try to write less number of line code and try to write effective lines.
If you need more help, please let me know. And if you find this answer suitable, Please vote me.
All the Best
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.
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];
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;
}
}