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

时间:2016-04-21 10:10:47

标签: ios swift2

尝试使我的Clear按钮工作,但作为一个新的程序员,我仍然有一些逻辑发现问题。 计算器工作得很好但是当我按下清除没有任何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()
  }
 }

3 个答案:

答案 0 :(得分:0)

再次查看您的代码 - 您有一张支票:

private static final String AUTHENTICATE_USER_TEST_URI = "/user/authenticate";

然后

if currentOperation != Operation.Empty { ...

第二个if的正文永远不会被执行!

答案 1 :(得分:0)

我不确定这个,但试试这个代码 可能对你有帮助

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

答案 2 :(得分:0)

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

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