Get an Int out of an UILabel Swift

后端 未结 6 1834
执笔经年
执笔经年 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:01

    connect all your buttons to 1 IBAction. then create the following variable and the set/get methods based on how you will use it.

    note: "something" is a UILabel. The variable I wrote below should help you do conversions easily and with cleaner syntax. "newValue" comes with all setter methods. It basically takes into account any value that could possibly used to set "num" to a new value.

     var num : Int {
         get { 
             return Int(something!)!
         }
         set {
             something.text = Int(newValue)
         }
    
     }
    

提交回复
热议问题