How to connect multiple buttons in a storyboard to a single action?
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:
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 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.
Swift3 Multiple buttons dynamic coding to trigger event
I prefer using the delegate design pattern when it comes to solving an issue like that for it I find it to be a much cleaner approach than just a mass amount of @IBActions
1- Create a Language class
import Foundation
class Language {
var id: Int
var name: String
init(id: Int, name: String) {
self.id = id
self.name = name
}
}
2- Create the custom cell in the storyboard or nib and then add the appropriate outlets and actions. And then you create a Delegate protocol for it
import UIKit
protocol CustomCellDelegate: class {
func customCell(newLanguageSelected language: Language)
}
class CustomCell: UITableViewCell {
var language: Language!
@IBOutlet weak var languageTextLabel: UILabel!
weak var delegate: CustomCellDelegate?
func setupCustomCell(withLanguage language: Language){
self.language = language
self.languageTextLabel.text = self.language.name
}
@IBAction func buttonPressed(sender: UIButton){
delegate?.customCell(newLanguageSelected: self.language)
}
}
3- Finally add the implementation in the cellForRow method of the UITableViewDataSource and add the implementation of the delegate in the UITableViewController class
import UIKit
class YourTableViewController: UITableViewController{
var languages: [Language] = []
//implement the other methods in the dataSource
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuse", for: indexPath) as! CustomCell
cell.delegate = self
cell.setupCustomCell(withLanguage: languages[indexPath.row])
return cell
}
}
extension YourTableViewController: CustomCellDelegate{
func customCell(newLanguageSelected language: Language) {
//Do what you will with the language
}
}
Good luck
Connecting Multiple Buttons to one action?
You can also set the tag property for each button in the interface builder and then use it to find out which button was pressed.... This also means that you don't need to define all your button references (UIButton) and keep track of them in code....
- (void) doSomething:(id)sender {
int buttonPressed = [sender tag];
switch (buttonPressed) {
case 0:....
// etc
}
}
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
How to Install iOS 7.0 and iOS 8.0 Simulators in Xcode 6.1
Leak from Nsurl and Avaudioplayer Using Arc
Typewriter Effect Text Animation
Present Uialertcontroller from Appdelegate
Adding Unlimited Lines in a Text (Swiftui)
Get User Profile Details (Especially Email Address) from Twitter in iOS
Alamofire Fire Variable Type Has No Subscript Members
Xcode Warning When Using Mapkit and Corelocation
Resize Superview After Subviews Change Dynamically Using Autolayout
Limit Number of Characters in Uitextview
When Does Awakefromnib Get Called
How to Fetch All Images from Custom Photo Album - Swift
How to Execute Some Code After a Segue Is Done
Disabling Firebase Automatic Screen Reporting
How to Save Created PDF in Document Folder and Merge in iOS