在UILabel中显示数字,操作员标志和总和

时间:2017-09-06 01:39:06

标签: ios swift

我对Xcode比较陌生。我正在制作一个骰子滚轮应用程序,其中可以按下代表六个不同骰子(从d4到d20)的六个按钮,并且最近拍摄的骰子的滚动显示在一个UILabel中。还有一个清除按钮,将标签重置为0.该部分已完成。

我遇到的问题是:我希望滚动的金额也显示在第二个UILabel中,显示每个滚动和按钮点击时的总和,直到用户点击清除按钮。例如,假设用户点击了d4并得到了一个3,d8得到了一个5,而d20得到了一个12--我想要第二个UILabel显示" 3 = 3", " 3 + 5 = 8",最后" 3 + 5 + 12 = 20"。

我知道如何编写基本的加法函数,如果我只是输出和本身,只是不确定如何使滚动同时显示在两个标签中以及第二个标签中的加号和等号。我并不需要明确说明的所有内容,只是为了指向正确的方向。我也是堆叠溢出的新手,所以如果我过于罗嗦或提出错误的问题,我会道歉。提前谢谢!

编辑下面我已经包含了一个到目前为止我的代码的图像和文本的链接 - 非常基本的,因为我只是在学习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.
    }
}

4 个答案:

答案 0 :(得分:1)

您还需要维护值的历史记录

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

答案 1 :(得分:1)

  

我希望第二个UILabel显示“3 = 3”,“3 + 5 = 8”,最后“3 + 5 + 12 = 20”。

你肯定需要一个能够跟踪最近骰子卷的数组;类型为[Int]的属性(整数数组)。

String的join()方法会将结果与您指定的分隔符(在本例中为“+”)连接起来。当tehere只有一个字符串要加入时(“3 = 3”,以上),它会自动使用不使用分隔符。

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

答案 2 :(得分:1)

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

以下是一些示例代码:

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
}

答案 3 :(得分:1)

看起来你在这里有一些很好的答案。我非常同意从class CustomScene : public QGraphicsScene { Q_OBJECT QGraphicsLineItem *item; protected: void mousePressEvent(QGraphicsSceneMouseEvent *event){ item = new QGraphicsLineItem; addItem(item); const QPointF p = event->scenePos(); item->setPos(p); } void mouseMoveEvent(QGraphicsSceneMouseEvent *event){ const QPointF p =item->mapFromScene(event->scenePos()); QLineF l = item->line(); l.setP2(p); item->setLine(l); } void mouseReleaseEvent(QGraphicsSceneMouseEvent *event){ const QPointF p =item->mapFromScene(event->scenePos()); QLineF l = item->line(); l.setP2(p); item->setLine(l); } }; 函数创建索引历史值的方法。如果我可以添加两美分,则可以选择使用事件处理程序IBAction。函数声明可能如下所示:

return dXRoll

然后你可以将@IBAction func dXTapped(sender: UIButton) -> Int { let dXRoll = arc4Random_uniform(X) + 1 rollLabel.text = "\(dXRoll)" return dXRoll } 放入存储容器(数组,元组,字典等);一个数组可能是最好的。为了保存历史值,我可能会尝试这样的事情(它更像是伪代码,因为我没有在我面前测试这段代码的Xcode):

dXRoll