有一个按钮减去我按下的下一个按钮的值。迅速

时间:2017-07-26 08:14:14

标签: ios swift

我的计数应用程序中有七个标记为1-7的按钮。计数从0开始,然后当我按下一个数字时,它被加到0.所以如果我按6,则计数为6.然后我按3,计数现在为9.现在我想要包含一个减法按钮,以便我可以通过点击减法按钮下来,然后选择我选择的值。因此,如果我按下减号按钮后跟数字2,则计数将返回到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) {



}

}

我不确定我应该在foulButton函数中添加什么代码。提前谢谢。

2 个答案:

答案 0 :(得分:1)

我首先给出"标签"对于每个按钮及其数值,您可以使用单个功能。然后,很容易模拟减法。

try{
    List<string> fileList = Directory.EnumerateFiles(folderPath, searchString)
                                .OrderByDescending(filename => filename)
                                .where(filename => filename.ToLower().EndsWith(".xml")
                                .ToList();
}
catch(Exception e){
    throw new Exception("Could not enumerate files.", e);
}
finally{
    //Close the stream?
}

try{
    XElement data = XElement.Load(pathToFile);
}
catch(Exception e){
    throw new Exception("Could not load xml file.", e);
}
finally{
    //Close the stream? The XmlReader?
}

答案 1 :(得分:0)

我认为您想要添加和减去值的按钮,您可以为此任务创建一个简单的通用函数: -

var count = 0
func addNumber(number:Int) {
    count = count+number
    var realNumber = String(number)
    print(realNumber)
    Score.text = realNumber
}

func subtractNumber(number:Int) {
    count = count-number
    var realNumber = String(number)
    print(realNumber)
    Score.text = realNumber
}


addNumber(number: 4)       //function call from your button
subtractNumber(number: 2)  //by respective values