简体   繁体   English

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

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

I'm a beginner programmer making my first real app, a calculator in Swift.我是一名初学者程序员,正在制作我的第一个真正的应用程序,Swift 中的计算器。

It's mostly working, but one issue I'm running into is how it displays numbers after pressing one of the operator buttons.它大部分都在工作,但我遇到的一个问题是它在按下一个操作员按钮后如何显示数字。 Currently, whenever an operator button is pressed, I have it set the label at the top of the calculator to "0".目前,每当按下操作员按钮时,我都会将计算器顶部的 label 设置为“0”。 But on actual calculators, this top display won't change until another number button is pressed.但是在实际的计算器上,这个顶部显示在按下另一个数字按钮之前不会改变。

If I don't reset the display to 0, then any number buttons that are pressed will add to the current text at the top, and mess up the equation that the calculator will have to do (ie 2+2 displays 22, and the solution it displays is 22+2=24)如果我不将显示重置为 0,那么按下的任何数字按钮都会添加到顶部的当前文本中,并且会打乱计算器必须执行的等式(即 2+2 显示 22,而它显示的解决方案是 22+2=24)

I'm wondering if it's possible to detect when one of the number buttons is pressed (listed in my code as the intButtonPressed IBAction) outside of the intButtonPressed function?我想知道是否可以检测到何时按下 intButtonPressed function 之外的数字按钮之一(在我的代码中列为 intButtonPressed IBAction)? That way I can keep the top label the same until the user starts inputting more text, then I can set it to 0 to prevent the calculator from breaking.这样我可以保持顶部 label 相同,直到用户开始输入更多文本,然后我可以将其设置为 0 以防止计算器崩溃。

Any other possible (better) solutions would be welcome as well任何其他可能的(更好的)解决方案也将受到欢迎

Here's my code:这是我的代码:

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()
    }
}

Update for anyone who finds this in the future: I've solved the issue, and realized that I wasn't very clear with what I wanted it to do.为将来发现此问题的任何人更新:我已经解决了这个问题,并意识到我不太清楚我想要它做什么。 I solved the problem simply by adding a condition to the if/else statement in the inButtonPressed function that detects if the topLabel is 0. By rewriting that to detect if the topLabel is 0 OR the same as num1, and then changing the else statement in the opButtonPressed function to set topLabel to String(num1), the program does exactly what I want.我通过在 inButtonPressed function 中的 if/else 语句中添加一个条件来解决问题,该条件检测 topLabel 是否为 0。通过重写该条件来检测 topLabel 是否为 0 或与 num1 相同,然后更改 else 语句opButtonPressed function 将 topLabel 设置为 String(num1),程序完全符合我的要求。 Here's the rewritten code:这是重写的代码:

@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.

I also added a separate function to handle decimals, and I had to update the code in there as well to keep that working.我还添加了一个单独的 function 来处理小数,并且我还必须更新其中的代码以保持其正常工作。

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

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