Assigning same action to multiple buttons at once - iphone

can I assign same action to multiple buttons in the Interface Builder once they are all selected by one connection?

This problem is discussed in numerous threads but it isn't always just laid out simply. The easiest route is to create a function such as the one below. It takes the sender, in this case a UIButton, and gets the tag. You can then run whatever code you want based on that.
- (IBAction) buttonClick: (id) sender {
UIButton *button = (UIButton *)sender;
int row = button.tag;
NSLog(#"Button clicked: %i", row);
if (row == 1 ) {
// do something
}
}
In Interface Builder, attach the button to the function, and then use the Attributes Inspector to set the Tag value for each button, giving each one a different integer value.

Related

Control more than one button using tags

I have 28 buttons in my application. I need to control them in a single function. All I want to do is make all the buttons visible. I gave tags to the buttons. I tried it with a for loop but I couldn't do it how can I solve this problem?
(IBAction)btnAction:(id)sender{
UIButton *btnPressed = (UIButton *)sender;
NSUInteger i=btnPressed.tag;
for(i=0; i<29; i++)
{
btnPressed.hidden=NO;
}
}
Instead of tags, use an IBOutletCollection. So now you have a single NSArray pointing to all of the buttons. Now just cycle through that NSArray.
You can create a single IBAction method, check tag values and then do what you want to do
- (IBAction)btnAction:(id)sender{
UIButton *btnPressed = (UIBUtton *)sender;
// Check button tags and write code accordingly //
}
use IBOutletCollection. see the following link for your answer.
IBOutletCollection
and
IBOutletCollection (UIbutton)
IBOutletCollection of UIButtons - changing selected state of buttons

how to pass parameter when different button call same class?

I am developing for iPhone the app I am developing has many buttons and I want all buttons to call the same class but with different parameter
for example I want button1 to call the displayimageclass
and button2 should call the same class displayimageclass so, how to different method call same class and in that same class how to fetch different button from where it is call to particular class.
That is pretty much standard. Just define as many IBAction methods as you need.
In the viewController's .h file:
- (IBAction)actionButton1:(UIButton *)sender;
- (IBAction)actionButton2:(UIButton *)sender;
- (IBAction)actionButton3:(UIButton *)sender;
And in its .m file:
{
//react here to button1 pressed
}
- (IBAction)actionButton1:(UIButton *)sender
{
//react here to button1 pressed
}
- (IBAction)actionButton2:(UIButton *)sender
{
//react here to button2 pressed
}
- (IBAction)actionButton3:(UIButton *)sender
{
//react here to button3 pressed
}
Then associate the action with the Button's events (TouchUpInside would be most appropriate, I guess).
You should add tag property to your buttons.
Suppose your buttons are
button1.tag =1;
button2.tag =2;
button3.tag =3;
When you click any button, NSLog shows related button tag number. i.e You will get your result what you want.
- (IBAction)buttonClicked:(id)sender {
UIButton* button = (UIButton *)sender;
NSLog(#"The number of tag:%d",button.tag);
}
I think it will be helpful to you.
you can implement your action method like this:
- (IBAction)action:(UIButton *)sender
{
//sender will be different for different button
}
Set tag value for buttons and get those buttons like
- (IBAction)action:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
}
Are you looking for
1) several buttons performing the same method with the same behavior
2) several buttons performing the same method but having different behavior based on the button characteristics
3) each button performing its own method.
1) You can register several buttons on the same IBAction, they all will call it and perform the same code
2) In the method were all the buttons are registered (cf: 1) it is possible to sort the buttons by subclassing the UIButton class, giving an enum value that represents what the button should perform and then in the called method check the enum value with a switch in order to trigger the wanted behavior
The UIBarButtonItem already has this tag property so you could use them by giving a value to each of your UIBarButtonItem in the Interface Builder and set them to call the following method
- (IBAction) myButtonsMethod:(id)object
if ([object isKindOfClass:[UIBarButtonItem class]])
{
switch ([(UIBarButtonItem *)object tag])
{
case myFirstTagValue
...
}
3) #Hermann Klecker solution is the right one.

Linking multiple buttons to one method with different jobs

I have a huge crazy scene in my story board that has 36 different buttons, and each one means something different when clicked on. I really don't want to go about creating 36 different methods, so how could I reference a button title or button name in a method that is called when one of the 36 buttons is pushed.
This is probably a simple question, but I'm new to iOS and Objective C...
Thanks!
You can create a single method, like so:
- (IBAction)buttonTapped:(id)sender{
// The button that was tapped is called "sender"
// This will log out the title of the button
//NSLog(#"Button: %#", sender.titleLabel.text);
//Edit: You need a cast in the above line of code:
NSLog(#"Button: %#", ((UIButton *)sender).titleLabel.text);
}
Then, you can use Interface Builder to connect to all of the buttons. You can have some sort of if/else logic to test which button was tapped.
You can check the titleLabel property, or you can assign an IBOutlet to each button and check for that.
For example:
if([sender isEqual:buttonOutlet1]){
//If this button is attached to buttonOutlet1
//do something
}
Alternatively, you can simply use the label of each button, not worrying about outlets.
A third option would be to generate and lay out the buttons in code, and then access them as elements of an array of buttons.
A fourth option would be to add tags to the buttons and check for the button's tag in your function.
Give each button a unique tag value. in the IBAction, sender.tag tells you which button was tapped.
The IBAction routine you set up to handle the button presses has a sender parameter. Examine that to decide.
-(IBAction) buttonPress: (id) sender {
UIButton *pressedButton = (UIButton *)sender;
NSString *buttonTitle = [pressedButton currentTitle];
if ([buttonTitle isEqualToString: #"SomeTitle"]) {
//do work for that button.
}
}
You can use a variety of NSString methods to compare or filter which button was pressed and handle it through if's or switches.
That's quite simple, but since you're new, here's an answer.
(According to Stanford cs193p course, 2010-2011 fall (that's what they did with the calculator app)) make a method that receives an argument, which is the UIButton.
for example:
- (IBAction) someMethodThatDoesSomething:(UIButton *)sender;
Then make if statements according to the sender.titleLabel.text
I don't know if there are any other solutions. Hope this helps!
-(IBAction)myButtonAction:(id)sender {
if ([sender tag] == 0) {
// do something here
}
if ([sender tag] == 1) {
// Do some think here
}
}
// in Other words
-(IBAction)myButtonAction:(id)sender {
NSLog(#"Button Tag is : %i",[sender tag]);
switch ([sender tag]) {
case 0:
// Do some think here
break;
case 1:
// Do some think here
break;
default:
NSLog(#"Default Message here");
break;
}

iphone sdk button tags?

I can't understand the logic og button tags. Can someone tell me how to use button tags?
For eg. There are two buttons on my view and I want to print something depending on their tags like:
if(button.tag==???)x{
}etc.
When you create the button, you can set it's tag.
myButton1.tag = 0;
myButton2.tag = 1;
Or if you're using interface builder, there's a field in the inspector to set the tag.
I assume you've linked the buttons to call the same action when they're pressed, or else you wouldn't be needing to distinguish by tag, so your method should look like:
- (IBAction)buttonPressed:(id)sender
{
UIButton *aButton = (UIButton *)sender; // we know the sender is a UIButton object, so cast it
if (aButton.tag == 0)
{
// button 1 pressed
}
else if (aButton.tag = 1)
{
// button 2 pressed
}
}
Yeah you can use the tag to retrieve the UIButtons, and apply the same logic with UIVIews (have a look at this method remembering that UIButton inherits from UIView).
Specifically where do you have problems? Can you post some code/pseudo-code of yours?

How to know or retrieve the sender id

I have the following method
-(IBAction)back:(id)sender {
}
and would like to be able to know the sender id.
e.g. if there are multiple buttons linked to this method, I would like to know which button was pressed.
Simply use the tag property, inherited from UIView, which is a NSInteger, in a switch statement, or using if conditions.
The tag property can be set in your code, or through InterfaceBuilder.
[sender tag]
I don't know what you mean by "id" (the "sender" is an id, effectively an NSObject *), but you could use tags. You have to set the tag beforehand in Interface Builder or programmatically.
If you have set up IBOutlets for the buttons in your interface then you can simply compare the sender to those.
That is in your interface definition if you have
...
(IBOutlet) UIButton *button1;
(IBOutlet) UIButton *button2;
...
and in your implementation you have:
- (IBAction) buttonPressed: (id) sender
{
if (sender == button1) {
....
}
else if (sender == button2) {
...
}
}
Personally, I'd prefer to use different action methods for each button and then they can all call a common routine for the things that are common. However, for simple projects the above will work.
-J
Set the tag property of each button to a unique integer (either in IB or programmatically) and switch on it inside your action method.