在Swift中,当按下IBAction外部的按钮时进行检测



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

它基本上是有效的,但我遇到的一个问题是,按下其中一个操作员按钮后,它如何显示数字。目前,每当按下操作员按钮时,我都会将计算器顶部的标签设置为";0〃;。但在实际的计算器上,这个顶部显示不会改变,直到按下另一个数字按钮。

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

我想知道是否可以检测在intButtonPressed函数之外按下其中一个数字按钮(在我的代码中列为intButtonPressed IBAction(的情况?这样,我可以保持顶部标签不变,直到用户开始输入更多文本,然后我可以将其设置为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函数中的if/else语句中添加一个条件来检测topLabel是否为0,就可以解决这个问题。通过重写它来检测topLabel是否为0或与num1相同,然后更改opButtonPressed函数中的else语句,将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.

我还添加了一个单独的函数来处理小数,我还必须更新其中的代码才能使其正常工作。

最新更新