Add same IBAction to multiple buttons. Latest Xcode, swift 3, iOS 10
This is a bug in Xcode. Hope apple gets its bugs fixed soon.
The default signature of the IBAction when you drag from button looks something like
@IBAction func meHere(_ sender: Any) {
//something here
}
Now if you add another button and try to set the same IBAction to the button it wont allow (Only apple knows why though)
Solution :
Change method signature to
@IBAction func meHere(_ sender: UIButton) {
}
Change Any to Specific UIComponent. Now control drag from your IBAction to your new button everything will be fine :)
As you can see both me and you button are hooked to same IBAction :)
Happy coding
How to use one IBAction for multiple buttons in Swift?
In Interface Builder, select the Attributes Inspector and set the Tag for each button with a unique number, then you can do something like this:
@IBAction changeLanguage(sender: AnyObject) {
guard let button = sender as? UIButton else {
return
}
switch button.tag {
case 1:
// Change to English
case 2:
// Change to Spanish
case 3:
// Change to French, etc
default:
print("Unknown language")
return
}
}
To connect the action to multiple buttons: in Interface Builder, right-click ViewController in the view hierarchy, then left-click to drag the action connection to each button.
IOS: one IBAction for multiple buttons
If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class.
You can then differentiate between the buttons within the IBAction method either by reading the text from the button...
- (IBAction)buttonClicked:(id)sender {
NSLog(@"Button pressed: %@", [sender currentTitle]);
}
...or by setting the tag
property in Xcode and reading it back via [sender tag]
. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.)
Xcode8 cannot connect multiple/single uibuttons to single @IBAction in swift file
By Using this workaround you can add existing action and also can connect multiple buttons to a single action.
I think there is a bug in Xcode8. You can add multiple/Single button to a single action /function by changing sender
to _ sender
eg :- Normal Button
@IBAction func huu(sender: UIButton) {
}
You can't add multiple buttons or single button to this action you need to simply change like this and then you can add multiple buttons by using drag and connect from storyboard.
@IBAction func huu(_ sender: UIButton) {
}
After connecting IBOutlets Xcode will show a warning like this :-
To remove this warning simple delete _
sign from the action/function. Make sure that to delete _
after connecting your IBOutlets
Hope that this will gonna help you! :)
How to make one IBAction that does the same action to two buttons when the first one is from a custom cell and the other one is from storyboard
I usually keep the backing UIView
s and UIViewController
s very thin in my applications and I create classes that do the actual heavy lifting. This makes for cleaner code and easier reuse.
class Player {
static func play() { // Static makes it easier to call from every class, assuming you only play one song at a time
// play logic goes here
}
}
class MyView: UIView {
@IBAction func play() { // connect in IB in your .xib
Player.play()
}
}
class MyViewController: UIViewController {
@IBAction func play() { // connect in IB in your storyboard
Player.play()
}
}
How to create multiple button on and off common codebase using Swift
You can create only one function
as below and connect all the button actions in storyboard
to this function
so that any button
will update its state.
@IBAction private func buttonAction(_ sender: UIButton) {
if button_isActive {
sender.backgroundColor = #colorLiteral(red: 0.1843137255, green: 0.6823529412, blue: 0.9764705882, alpha: 1)
sender.setTitleColor(UIColor.white, for: .normal)
} else {
sender.backgroundColor = #colorLiteral(red: 0.80803, green: 0.803803, blue: 0.805803, alpha: 1)
sender.setTitleColor(UIColor.darkGray, for: .normal)
}
button_isActive = !button_isActive
// To differentiate different buttons
switch (sender.tag) {
case 0:
print(sender.title(for: .normal))
case 1:
print(sender.title(for: .normal))
default:
print(sender.title(for: .normal))
}
}
You can also set tag
for each in the storyboard
and differentiate in above method to know what button
click is this.
Related Topics
Ios13 Share Sheet: How to Set Preview Thumbnail When Sharing Uiimage
Typewriter Effect Text Animation
How to Parse Iso 8601 Using Nsdateformatter with Optional Milliseconds Part
Marking Some Xib/Storyboard Strings as Not Localizable
Limit Formatted Measurement to 2 Digits
Strange Constraints Behaviour on iPad
How to Launch the iOS Simulator from Terminal
Xcode - How to Drag a Component from One View to Another Without Losing Its Frame
Get Country Code from Country Name in iOS
How to Fetch All Images from Custom Photo Album - Swift
Why Would a 'Scheduledtimer' Fire Properly When Setup Outside a Block, But Not Within a Block
Detect App Launch from Widgetkit Widget Extension
Is There a Simple Way to Edit/Modify a Uilocalnotification
How to Change the Color of Uipickerview Selector
Reading Text and Images from a PDF Document in iOS
Prevent Users from Modifying Part of the Text in Slcomposeviewcontroller