ios - Have a button subtract the value of the next button I press. Swift -
i have 7 buttons labeled 1-7 in counting app. counting starts @ 0, when press number, added onto 0. if press 6, count @ 6. press 3, , count @ 9. want include subtraction button can go down hitting subtract button, picking value of choice. if hit subtraction button followed number 2, count go 7.
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var score: uilabel! var number = 0 @ibaction func onebutton(_ sender: uibutton) { var number1 = number + 1 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func twobutton(_ sender: uibutton) { var number1 = number + 2 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func threebutton(_ sender: uibutton) { var number1 = number + 3 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func fourbutton(_ sender: uibutton) { var number1 = number + 4 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func fivebutton(_ sender: uibutton) { var number1 = number + 5 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func sixbutton(_ sender: uibutton) { var number1 = number + 6 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func sevenbutton(_ sender: uibutton) { var number1 = number + 7 number = number1 var realnumber = string(number) score.text = realnumber } @ibaction func foulbutton(_ sender: uibutton) { } }
i not sure code should putting in foulbutton function. thank in advance.
i'd start giving "tags" each of ur buttons numeric value, can use single function all. easy simulate subtraction.
var subtraction = false @ibaction func buttonaction(_ sender: uibutton) { var change = sender.tag if subtraction { change = change * (-1) subtraction = false } number = number + change score.text = string(number) } @ibaction func foulbutton(_ sender: uibutton) { subtraction = !subtraction }
Comments
Post a Comment