How to connect multiple buttons in a storyboard to a single action?
Solution 1
You can connect multiple buttons to a single action without selecting them all at once.
However, after creating the action, you need to save the source file containing the action before Xcode will let you connect another control to the action.
I did this with Xcode 4.6.2:
Solution 2
Swift 3.0 Xcode 8.0+
Connect all buttons to below method signature. Make sure argument sender is of type UIButton (Or subclass of UIButton) in-order to connect rest of the buttons from storyboard.
@IBAction func dialconfigTapped(_ sender: UIButton) {}
Use sender.tag to identify the tapped button.
Solution 3
Follow this steps
1. Create IBAction In View Controller
In .h file
-(IBAction)action:(id)sender;
In .m File
-(IBAction)action:(id)sender{
}
2 . Connect all button to this action
3 . Give Each Button a tag by follow the next Picture
4 . Now inside your action function put these
-(IBAction)action:(id)sender{
UIButton *button=(UIButton*)sender;
if(button.tag==1){
//your stuff!
}
if(button.tag==2){
//your stuff!
}
if(button.tag==3){
//your stuff!
}
}
Run and Go.
Solution 4
You should be able to do this in IB. In IB, simply point them at the same IBAction
in the relevant class.
When the action comes, in case you want to know from which button you got the action, you can then differentiate between the buttons within the IBAction
method either by reading the text from the button or using the tag
attribute which you should have set from IB.
- (IBAction)buttonClicked:(id)sender {
NSLog(@"Button pressed: %@", [sender currentTitle]);
NSLog(@"Button pressed: %@", [sender tag]);
}
jab
Updated on December 30, 2020Comments
-
jab over 3 years
I'm currently using Xcode 4.6 and I'm simply wondering how to select multiple buttons across different UIViews, but under a single view controller. CMD clicking doesn't seem to work. I need this functionality because in Xcode 4.6 the only way to let two buttons on IB to share the same action is to select them at once and then drag the action to your view controller.
My ultimate goal is to get two different buttons on two different UIViews to match the same action using storyboards in Xcode 4.6. Is there a way to do this?
EDIT: I'm currently using Xcode 4.6.1 without any luck, upgrading to 4.6.2.
-
jab about 11 yearsIt seems that there is a bug in Xcode 4.6 though that won't allow you to do this. It's explained on this question, with a comment to the accepted answer. stackoverflow.com/questions/5858247/…
-
Srikar Appalaraju about 11 yearsbut isn't there a solution posted too ? - "Just realized that you can also multiselect all the UIButtons that you have created in IB by holding CMD and selecting them all, then ctrl-drag that to the code, and it will create a single IBAction with all the UIButtons connected to it." does this not work for you?
-
jab about 11 yearsNo it doesn't, the UIButtons are across multiple views, though they share the same view controller. CMD clicking these UIButtons will not allow me to select multiples.
-
jab about 11 yearsHmm, I'm currently using Xcode 4.6.1, let me see if upgrading fixes this.
-
rob mayoff about 11 yearsYou may also find that after saving, you need to wait a few seconds for Xcode to catch up.
-
jab about 11 yearsYeah, I saved many times (the file I was adding to had existed for quite a while with the action). It just never worked for me on 4.6.1. It works fine on 4.6.2. +1
-
rob mayoff about 11 yearsAnother thing that sometimes helps is the File > Close “ViewController.m” menu command (substitute the name of your currently-selected file; the shortcut is Control-Command-W). Then reopen the file. I find this often causes Xcode to notice things it was ignoring. In the Assistant Editor, that menu command is only available if the jump bar is set to “Manual”.
-
anyone about 10 years@Chris : Xcode : 5.0.2 : no need to save before assigning action.
-
Chris about 10 yearsI use 5.0.2, and find I need to - it's screwed me over a few times when I don't expect Xcode to be that inconsistent. =/
-
Supertecnoboff over 8 years@Chris I still do this in Xcode 6 and I presume it won't be much different in Xcode 7 too.
-
wuf810 over 7 yearsBut be aware this by default connects the action to the "Primary Action" event for the control. In this case for a UIButton it attaches to a Touch up Inside event. If you want to attach an action for a different event (say a Touch down event action for example), the best way is select the view itself, select the Connections inspector and the click the little + button for the required action and drag onto the button you want to attach it too.
-
rob mayoff over 7 yearsOr you can right-click or control-click on the button to open a floating window listing the button's connections.