显示uilabel中的数字,操作符号和总和



我对Xcode是相对较新的。我正在制作一个骰子滚筒应用程序,其中可以按下六个代表六个不同骰子(从D4到D20)的按钮,并且最近敲击的骰子的滚动显示在一个Uilabel中。还有一个清晰的按钮将标签重置为0。该部分已完成。

我遇到的麻烦是:我希望滚动的金额也出现在第二个uilabel中,该量子显示每个滚动卷,并在按钮被敲击时显示总和,直到用户点击了清除按钮。例如,说用户敲击了D4并获得了3,D8并获得了5,并且D20得到了12-我希望第二个Uilabel显示" 3 = 3"," 3 5 = =8英寸,最后是" 3 5 12 = 20"。

我知道如何编程基本添加功能,如果我只是输出总和本身,只是不确定如何将滚动与PLUS一起在两个标签中同时显示并等于第二个标签中的符号。我不需要明确拼写的所有内容,只是要指向正确的方向。我也是堆栈溢出的新手,所以如果我太word句或提出错误类型的问题,我深表歉意。预先感谢!

edit 下面的我在远基本的情况下包括了我的代码的链接以及文本,因为我只是在学习xcode和swift。

import UIKit
class ViewController: UIViewController {
    //Sum label and Roll label
    @IBOutlet weak var rollLabel: UILabel!
    @IBOutlet weak var sumLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    //Dice/Clear buttons
    @IBAction func d20Tapped(_ sender: UIButton) {
        let d20Roll = arc4random_uniform(20) + 1
        rollLabel.text = "(d20Roll)"   
    }
    @IBAction func d12Tapped(_ sender: UIButton) {
        let d12Roll = arc4random_uniform(12) + 1
        rollLabel.text = "(d12Roll)"
    }
    @IBAction func d10Tapped(_ sender: UIButton) {
        let d10Roll = arc4random_uniform(10) + 1
        rollLabel.text = "(d10Roll)"
    }
    @IBAction func d8Tapped(_ sender: UIButton) {
        let d8Roll = arc4random_uniform(8) + 1
        rollLabel.text = "(d8Roll)"
    }
    @IBAction func d6Tapped(_ sender: UIButton) {
        let d6Roll = arc4random_uniform(6) + 1
        rollLabel.text = "(d6Roll)"
    }
    @IBAction func d4Tapped(_ sender: UIButton) {
        let d4Roll = arc4random_uniform(4) + 1
        rollLabel.text = "(d4Roll)"
    }
    @IBAction func clearButtonTapped(_ sender: UIButton) {
        rollLabel.text = "0"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我希望第二个Uilabel显示" 3 = 3"," 3 5 = 8",最后是" 3 5 12 = 20"。

您肯定需要一个阵列,以跟踪最近的底卷;类型[Int]的属性(整数数组)。

有一种join()字符串方法,它将与指定的分离器相连(在这种情况下为" ")。当tehere只有一个字符串即可加入时,它会自动考虑使用不使用分离器(上方" 3 = 3")。

从您的问题中尚不清楚您是否只需要最后一个三个卷或更多?

这是这样做的一种方法,但是您可以将值存储在数组中,并在每个按钮上按更新标签和带有新骰子滚动结果的阵列。

这是一些示例代码:

var results = [Int]()
@IBAction func didTapButton(sender: Any) {
    var lastResult = lastDiceRollResult()
    results.append(lastResult)
    // Map goes through each item in result and lets you perform
    // an operation on it then return it. And creating a new array
    // of the returned values.
    // Then the new array is joined by "+". Where if results = [1,2,3]
    // expression would be: "1+2+3"
    var expression = results.map { String($0) }.joined(separator: "+")
    // Then we append an "=" and reduce the values of results
    // into 1 one final value: the sum of results
    expression += "=" + String(results.reduce(0) { $0 + $1 })
    // expression should look like: "1+2+3=6"
    resultLabel.text = expression
}

您需要维护值的历史

var history: [Int] = []
var labelText = "" // your second label text
for number in history {
    labelText.append(String(number))
    if history.last == number {
        labelText.append("=")
    } else {
        labelText.append("+")
    }
}
labelText.append(String(history.reduce(0, +)))
sumLabel.text = labelText

似乎您在这里有一些不错的答案。我强烈同意从IBAction函数中创建索引历史值的方法。如果我可以添加两分钱,则可以选择事件处理程序return dXRoll。功能声明看起来像这样:

@IBAction func dXTapped(sender: UIButton) -> Int {
    let dXRoll = arc4Random_uniform(X) + 1
    rollLabel.text = "(dXRoll)"
    return dXRoll
}

然后,您可以将dXRoll放入存储容器(数组,元组,字典等)中;阵列可能在这里最好。为了保存历史值,我可能会尝试这样的事情(这更像是伪代码,因为我面前没有Xcode来测试此代码):

let dXArrays = [4,6,8,10,12,20]; var historicalData:[Int] = []
func saveHistoricalValues() -> Array<Int> {
    for index in 0..<dXArrays.count {
        var currentDXArray = dXArrays[index]
        if currentDXArray === nil { //You can probably replace this with the ternary conditional operator, but I am very unfamiliar with that, so I did not put it in the answer.
            continue
        } else {
            historicalData.append(dXRoll[dXArrays[currentDXArray]]) //Pseudocode
        }
    }
    return historicalData
}

最新更新