繁体   English   中英

检测何时在 Swift 中的 IBAction 之外按下按钮?

[英]Detect when a button is pressed outside of the IBAction in Swift?

我是一名初学者程序员,正在制作我的第一个真正的应用程序,Swift 中的计算器。

它大部分都在工作,但我遇到的一个问题是它在按下一个操作员按钮后如何显示数字。 目前,每当按下操作员按钮时,我都会将计算器顶部的 label 设置为“0”。 但是在实际的计算器上,这个顶部显示在按下另一个数字按钮之前不会改变。

如果我不将显示重置为 0,那么按下的任何数字按钮都会添加到顶部的当前文本中,并且会打乱计算器必须执行的等式(即 2+2 显示 22,而它显示的解决方案是 22+2=24)

我想知道是否可以检测到何时按下 intButtonPressed function 之外的数字按钮之一(在我的代码中列为 intButtonPressed IBAction)? 这样我可以保持顶部 label 相同,直到用户开始输入更多文本,然后我可以将其设置为 0 以防止计算器崩溃。

任何其他可能的(更好的)解决方案也将受到欢迎

这是我的代码:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var topLabel: UILabel!
    
    var num1 = Double()
    var solution = Double()
    var op = String()
    
    @IBAction func intButtonPressed(_ sender: UIButton) {
        if topLabel.text == "0" {
            topLabel.text = sender.currentTitle
        }
        else if topLabel.text == String(solution) {
            num1 = Double(topLabel.text!)!
            solution = 0.00
            topLabel.text = sender.currentTitle
                // Basically stops user from adding to solution?
                // Also stores solution as num1 and clears solution field
        }
        else {
            topLabel.text = topLabel.text! + sender.currentTitle!
            // Keep typing
        }
        
        if (topLabel.text?.count)! > 12 {
            topLabel.text = "Error"
        }
        else {
            return
        }
        // if its greater than 12 characters, display "error"
    }

    @IBAction func clearButtonPressed(_ sender: UIButton) {
        num1 = 0.00
        solution = 0.00
        topLabel.text = "0"
    }
    
    @IBAction func opButtonPressed(_ sender: UIButton) {
        if sender.currentTitle == "=" {
            equals()
        }
        else {
            op = sender.currentTitle!
            num1 = Double(topLabel.text!)!
            topLabel.text = "0"
        }
        // Need it to display num1 until I press another button, then I need it to only display that text.
    }
    
    func equals() {
        switch op {
        case "+":
            add()
        case "-":
            subtract()
        case "×":
            multiply()
        case "÷":
            divide()
        default:
            print(Error.self)
        }
    }
    
    func add() {
        solution = num1 + Double(topLabel.text!)!
        topLabel.text = String(solution)
    }
    
    func subtract() {
        solution = num1 - Double(topLabel.text!)!
        topLabel.text = String(solution)
    }
    
    func multiply() {
        print("topLabel = ", topLabel.text!)
        solution = num1 * Double(topLabel.text!)!
        print("solution = ", solution)
        topLabel.text = String(solution)
    }
    
    func divide() {
        solution = num1 / Double(topLabel.text!)!
        //answer()
    }
}

为将来发现此问题的任何人更新:我已经解决了这个问题,并意识到我不太清楚我想要它做什么。 我通过在 inButtonPressed function 中的 if/else 语句中添加一个条件来解决问题,该条件检测 topLabel 是否为 0。通过重写该条件来检测 topLabel 是否为 0 或与 num1 相同,然后更改 else 语句opButtonPressed function 将 topLabel 设置为 String(num1),程序完全符合我的要求。 这是重写的代码:

@IBAction func intButtonPressed(_ sender: UIButton) {
        if topLabel.text == "0" || topLabel.text == "0.0" || topLabel.text == String(num1){
            dotPressed = false
            // Resets dotPressed whenever the top is 0 or num1 and an int button is pressed
            topLabel.text = sender.currentTitle
        }
        else if topLabel.text == String(solution) {
            num1 = Double(topLabel.text!)!
            solution = 0.00
            topLabel.text = sender.currentTitle
                // Basically stops user from adding to solution?
                // Also stores solution as num1 and clears solution field
        }
        else {
            topLabel.text = topLabel.text! + sender.currentTitle!
        }
}

@IBAction func opButtonPressed(_ sender: UIButton) {
        
        if sender.currentTitle == "=" {
            equals()
        }
        else {
            op = sender.currentTitle!
            num1 = Double(topLabel.text!)!
            topLabel.text = String(num1)
        }
        // Successfully displays num1 until I press another button, then only displays that text.

我还添加了一个单独的 function 来处理小数,并且我还必须更新其中的代码以保持其正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM