计算器中没有响应的清除按钮

问题描述 投票:1回答:3

试图使我的清除按钮工作,但作为一个新的程序员我仍然有一些逻辑发现问题。计算器工作正常,但当我按清除没有任何ID发生。

class ViewController: UIViewController {

enum Operation: String {
    case Divide = "/"
    case Multiply = "*"
    case Subtract = "-"
    case Add = "+"
    case Empty = "Empty"

}

@IBOutlet weak var outputLbl: UILabel!

var btnSound: AVAudioPlayer!

var runningNumber = ""
var leftValStr = ""
var rightValStr = ""
var currentOperation: Operation = Operation.Empty
var result = ""


override func viewDidLoad() {
    super.viewDidLoad()

    let path = NSBundle.mainBundle().pathForResource("btn", ofType: "wav")
    let soundUrl = NSURL(fileURLWithPath: path!)

    do{
        try btnSound = AVAudioPlayer(contentsOfURL: soundUrl)
        btnSound.prepareToPlay()
    } catch let err as NSError{
    print(err.debugDescription)
    }
}

@IBAction func numberPressed(btn: UIButton!){
   playSound()
    runningNumber += "\(btn.tag)"
    outputLbl.text = runningNumber
}

@IBAction func onDividePressed(sender: AnyObject) {
    processOperation(Operation.Divide)
}

@IBAction func onMultiplyPressed(sender: AnyObject) {
    processOperation(Operation.Multiply)
}

@IBAction func onSubtractPressed(sender: AnyObject) {
    processOperation(Operation.Subtract)
}

@IBAction func onAddPressed(sender: AnyObject) {
    processOperation(Operation.Add)
}

@IBAction func onEqualPressed(sender: AnyObject) {
    processOperation(currentOperation)
}

@IBAction func onClearPressed(sender: AnyObject) {
    processOperation(Operation.Empty)
}

func processOperation(op: Operation) {
    playSound()

    if currentOperation != Operation.Empty{
        //run math

        //a user selected an operator, but then selected another operator without
        //first entering a number
        if runningNumber != ""{

        rightValStr = runningNumber
        runningNumber = ""

        if currentOperation == Operation.Multiply{
        result = "\(Double(leftValStr)! * Double(rightValStr)!)"
        } else if currentOperation == Operation.Divide{
        result = "\(Double(leftValStr)! / Double(rightValStr)!)"
        } else if  currentOperation == Operation.Subtract{
        result = "\(Double(leftValStr)! - Double(rightValStr)!)"
        } else if currentOperation == Operation.Add{
        result = "\(Double(leftValStr)! + Double(rightValStr)!)"
        } else if currentOperation == Operation.Empty{

            result = ""
            outputLbl.text = result


            }

        leftValStr = result
        outputLbl.text = result

        }


        currentOperation = op


    }else{
        //first time its been pressed
        leftValStr = runningNumber
        runningNumber = ""
        currentOperation = op
    }
}

func playSound() {
    if btnSound.playing{
        btnSound.stop()
    }
    btnSound.play()
  }
 }
ios swift2
3个回答
0
投票

我不确定这一点,但尝试这个代码可能会帮助你

@IBAction func onClearPressed(sender: AnyObject) 
{
      outputLbl.text = @"";
    //processOperation(Operation.Empty)
}

0
投票

再看一下你的代码 - 你有一张支票:

if currentOperation != Operation.Empty { ...

然后

   ... else if currentOperation == Operation.Empty {

第二个的身体永远不会被执行!


0
投票
else{
    //first time its been pressed
    leftValStr = runningNumber
    runningNumber = ""
    currentOperation = op
    outputLbl.text = ""
}

在你的其他部分,你必须清除标签。首先,检查操作是否为空,并在正文中定义相同的操作。

© www.soinside.com 2019 - 2024. All rights reserved.