Get an Int out of an UILabel Swift

后端 未结 6 1823
执笔经年
执笔经年 2021-01-20 17:22

I have the problem, to have a high amount of buttons which have a number as their label, so i thought i could take the label as an integer instead of creating an action for

6条回答
  •  伪装坚强ぢ
    2021-01-20 18:19

    If you want to do this, you can convert the string to an Int by using string.toInt() such as:

    if let input = sender.titleLabel?.text?.toInt() {
        // do something with input
    } else {
        // The label couldn't be parsed into an int
    }
    

    However, I'd suggest either using UIView.tag or subclassing UIButton and adding an Int property to it to accomplish this, in case you ever change the display of your labels.

提交回复
热议问题