Im trying to make a generator for my buttons/actions. How could I do this? >
var buttonPairs = [
[“Reset”,handleReset] // handleHeightReset - function in current self
]
for data in buttonPairs{
mButtonPtrs[data[0] as! String] = UIButton()
mButtonPtrs[data[0]!.addTarget(self, action: #selector(data[1]) , for: UIControl.Event.touchUpInside)
}
I keep getting error :
swift Argument of ‘#selector’ does not refer to an ‘@objc’ method, property, or initializer
You can try this -
import Foundation
import UIKit
struct ButtonConfig {
let title: String
let action: Selector
}
class ViewController: UIViewController {
@objc func handleReset() {}
var buttonConfigs: [ButtonConfig] = [
.init(title: "Reset", action: #selector(handleReset))
]
var buttonsCache: [String: UIButton] = [:]
func prepareButtonsCache() {
for config in buttonConfigs {
let button = UIButton()
button.addTarget(self, action: config.action, for: .touchUpInside)
buttonsCache[config.title] = button
}
}
}