Is there any way of sending string value to UIButton tag? I know tag is Int type but I need to send string value.
let myButton: UIButton = {
    let button = UIButton()
    button.tag = "123"
    return button
}()
I need to send button.tag as a string
Create a custom class of UIButton and create one String type property like customTag
class CustomButton: UIButton {
    var customTag:String = ""
}
Assign the custom class to your UIButton and use String type tag by following code.
let myButton: CustomButton = {
    let button = CustomButton()
    button.customTag = "ABC"
    return button
}()
                        Instead of tag you can do it using accessibilityLabel.
btn.accessibilityLabel = "ABC"
                        One more answer.
Instead of accessibilityLabel / accessibilityIdentifier or any other related to accessibility , let us go for layer.name
Button.layer.name = "abc123"
@IBAction func wasPressed(_ sender: UIButton) {
   print("was Pressed     \(sender.layer.name)")
}
Reason:
Incase, we are in need to set accessibility for each and every screen, at that time it might affect.
But, if we are in need to add another layer for UIButton, it will not affect
Unfortunatly, the tag is just an integer value. What you could do is:
UIButton and add your own propertyaccessibilityIdentifier
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With