如何使用其他按钮添加到UIStepper值?

时间:2015-10-25 04:16:14

标签: ios swift swift2 uistepper

我有UIStepper递增/递减1到UILabel,我希望有一个额外的+3按钮,当按下+3时它会添加3到标签,但是如果你按下UIStepper它会回到1.

我的代码如下:

import Foundation
import UIKit
import AVFoundation


class ViewTwo : UIViewController, AVAudioPlayerDelegate {

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }

    /// Actions de sonido -------
    @IBAction func stepperSoundRed(sender: UIStepper) {
        audioPlayer.play()
    }
    @IBAction func stepperSoundBlue(sender: UIStepper) {
        audioPlayer.play()
    }
    /////////////////////////

    // el nombre del equipo Blue
    @IBOutlet weak var teamBlueTextLabel: UILabel!
    var BlueName = String()

    // nombre del equipo rojo
    @IBOutlet weak var teamRedTextLabel: UILabel!
    var RedName = String()

    // score inicial del equipo rojo
    @IBOutlet weak var RedScoreLabel: UILabel!
    var RedScore = String()

    // score initcial equipo azul
    @IBOutlet weak var BlueScoreLabel: UILabel!
    var BlueScore = String()

    // que funcionen los Steppers
    @IBOutlet weak var RedStepperUI: UIStepper!
    @IBOutlet weak var BlueStepperUI: UIStepper!

    // Botón Done
    @IBOutlet weak var BotonDone: UIButton!

    @IBAction func BlueStepperValueChange(sender: UIStepper) {
        BlueScoreLabel.text  = Int(sender.value).description
    }

    @IBAction func RedStepperValueChange(sender: UIStepper) {

        RedScoreLabel.text  = Int(sender.value).description

    }


    @IBOutlet weak var periodUILabel: UILabel!





   // archivos de sonidos
    var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ping2", ofType: "mp3")!)
    var audioPlayer = AVAudioPlayer()

    required init? (coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
        do {
            try audioPlayer = AVAudioPlayer(contentsOfURL: sound, fileTypeHint: nil)
            audioPlayer.prepareToPlay()
            audioPlayer.delegate = self
            //audioPlayer.play()

        } catch {
            // Errors here
        }
    }
    @IBAction func BackToViewOne(sender: UIButton) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    override func shouldAutorotate() -> Bool {
        return true
    }
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Landscape
    }

    // Period


    @IBOutlet weak var periodUILabel2: UILabel!

    @IBAction func periodAdd1(sender: UIButton) {

        if(periodUILabel.text == "20") {

        }
        else {
            let period = Int(periodUILabel.text!)!+1
            periodUILabel.text = String(period)
        }

    }

    @IBAction func periodMinus1(sender: UIButton) {

        if(periodUILabel.text == "0") {

        }
        else {
            let period = Int(periodUILabel.text!)!-1
            periodUILabel.text = String(period)
        }
    }

    // VIEW DID LOAD

    override func viewDidLoad() {


        // No se para que es esto??
        super.viewDidLoad()

        BotonDone.layer.cornerRadius = 5

        // Checamos si están vacios los nombres
        if BlueName.isEmpty {
            BlueName = "BLUE"
        }

        if RedName.isEmpty {
            RedName = "RED"
        }
        if RedScore.isEmpty {
            RedScore = "0"
        }
        if BlueScore.isEmpty {
            BlueScore = "0"
        }


        // Proseguimos a asignarlos a las Labels

        teamBlueTextLabel.text = BlueName
        teamRedTextLabel.text = RedName
        RedScoreLabel.text = RedScore
        BlueScoreLabel.text = BlueScore


        // Aqui vemos el Red +/-
        RedStepperUI.wraps = true
        RedStepperUI.autorepeat = false
        RedStepperUI.value = Double(RedScore)!
        RedStepperUI.maximumValue = 999

        // aqui vemos el Blue +/-
        BlueStepperUI.wraps = true
        BlueStepperUI.autorepeat = false
        BlueStepperUI.value = Double(BlueScore)!
        BlueStepperUI.maximumValue = 999

        //
        let value = UIInterfaceOrientation.LandscapeLeft.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }


    // BOTONES MAS +3

    @IBAction func RedPlus3UIButton(sender: UIButton) {
        let CurrentScoreRed = RedStepperUI.value;
        RedScoreLabel.text = String (Int(CurrentScoreRed) + 3)
        audioPlayer.play()
    }


    @IBAction func BluePlus3UIButton(sender: UIButton) {

        audioPlayer.play()
    }


    // BOTONES + Y -


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let DestViewController : ViewThree = segue.destinationViewController as! ViewThree

        DestViewController.FinalScoreBlue = BlueScoreLabel.text!
        DestViewController.FinalScoreRed = RedScoreLabel.text!
        DestViewController.TeamNameR1Label = teamRedTextLabel.text!
        DestViewController.TeamNameB1Label = teamBlueTextLabel.text!
    }
}









class MyButtons : UIButton {
   required init(coder aDecoder: (NSCoder!)) {
     super.init(coder: aDecoder)!
        self.layer.cornerRadius = 5
        self.layer.borderColor = UIColor.whiteColor().CGColor
        self.layer.borderWidth = 1
    }
}

这是+3按钮:

@IBAction func RedPlus3UIButton(sender: UIButton) {
    let CurrentScoreRed = RedStepperUI.value;
    RedScoreLabel.text = String (Int(CurrentScoreRed) + 3)
    audioPlayer.play()
}

1 个答案:

答案 0 :(得分:0)

您需要更新步进器的value属性。例如:

@IBAction func RedPlus3UIButton(sender: UIButton) {
    RedStepperUI.value += 3
    let CurrentScoreRed = RedStepperUI.value
    RedScoreLabel.text = String(Int(CurrentScoreRed))
    audioPlayer.play()
}