如何将多个UIStepper值添加到一个Label?

时间:2015-10-26 19:58:05

标签: swift label add uistepper

我现在正在第一个Swift项目上工作。我有两个步进器和一个标签 - 两个步进器都将它们的值发送给它。如何将第二个步进器的值添加到标签中,其中第一个步进器的值已经存在?这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()


    stepper.wraps = true
    stepper.autorepeat = true
    stepper.maximumValue = 10000
    stepper2.wraps = true
    stepper2.autorepeat = true
    stepper2.maximumValue = 10000

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var valueLabel: UILabel!
@IBOutlet weak var stepper: UIStepper!
@IBAction func stepperValueChanged(sender: UIStepper) {
    valueLabel.text = Int(sender.value).description
}



@IBOutlet weak var stepper2: UIStepper!
@IBAction func stepper2ValueChanged(sender: UIStepper) {
    valueLabel.text = Int(sender.value).description
}

}

谢谢!

1 个答案:

答案 0 :(得分:0)

如果要将两个值组合为一个字符串并在Label上显示此字符串,则必须创建一个为您执行此操作的新函数。我在你的代码中添加了这样一个函数:`

类ViewController:UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    stepper.wraps = true
    stepper.autorepeat = true
    stepper.maximumValue = 10000
    stepper2.wraps = true
    stepper2.autorepeat = true
    stepper2.maximumValue = 10000

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var valueLabel: UILabel!
@IBOutlet weak var stepper: UIStepper!

@IBAction func stepperValueChanged(sender: UIStepper) {

    // valueLabel.text = Int(sender.value).description
    addValuesToASumAndPutItIntoTheLabel()
}

@IBOutlet weak var stepper2: UIStepper!
@IBAction func stepper2ValueChanged(sender: UIStepper) {

    // valueLabel.text = String(sender.value)
    addValuesToASumAndPutItIntoTheLabel()

}

func addValuesToASumAndPutItIntoTheLabel() {
    let summe : Int = Int(stepper.value + stepper2.value)
    valueLabel.text = summe.description
}

}`