简体   繁体   English

斯威夫特:为什么我的-1按钮无法正常工作?

[英]Swift: Why isn't my -1 button working properly?

I have a +1 button and a -1 button and a label that starts at 0. My +1 button is working great, but for some reason, my -1 button isn't working. 我有一个+1按钮和一个-1按钮,以及一个从0开始的标签。我的+1按钮运行良好,但由于某种原因,我的-1按钮无法正常工作。 Can anyone help? 有人可以帮忙吗?

var sales = 0

@IBOutlet weak var numberOfSalesLabel: UILabel!
@IBOutlet weak var minusOneSaleOutlet: UIButton!

@IBAction func plusOneSale(sender: AnyObject) {
    sales += 1
    numberOfSalesLabel.text = "\(sales)"
    if sales >= 1 {
        minusOneSaleOutlet.hidden = false
       }
}

override func viewDidLoad() {
}

@IBAction func minusOneSale(sender: AnyObject) {
    sales -= 1
    numberOfSalesLabel.text = "\(sales)"

    if sales == 0 {
        minusOneSaleOutlet.hidden = true
    }
}

Anybody got any ideas why my minus button isn't working? 有人知道为什么我的减号按钮不起作用吗? I'm thinking it might have to do with me calling it as an outlet and as an action, but I'm not sure. 我认为这可能与我将其称为出口和行动有关,但我不确定。 Thanks! 谢谢!

ps-I'm not sure if this is normal. ps-我不确定这是否正常。

在此处输入图片说明

Using the connections inspector you'll need to make sure all the connections are correct. 使用连接检查器,您需要确保所有连接正确。

If at any point you delete an @IBAction from your code, then create another connection, the old one will still remain until you remove it properly from the connection inspector. 如果您随时从代码中删除@IBAction,然后创建另一个连接,则旧的连接将一直保留,直到您从连接检查器中将其正确删除为止。

Each of your buttons should only be connected to a single @IBAction (touchUpInside) 您的每个按钮应仅连接到单个@IBAction(touchUpInside)

Below is what the connections inspector looks like. 下面是连接检查器的外观。 The image is from one of my own projects, and shows a connected delegate. 该图像来自我自己的项目之一,并显示了一个已连接的委托。 So yours will look a bit different. 因此,您的外观会有所不同。

If all else fails, remove all connections on this view (click the x) and link them up again by Ctrl dragging from your button, into the code viewer right on top of your @IBAction. 如果所有其他方法都失败,请删除该视图上的所有连接(单击x),然后通过按住Ctrl键从您的按钮拖动到@IBAction顶部的代码查看器中,再次将它们链接起来。 Sometimes its just easier to start again. 有时候,重新开始更容易。

在此处输入图片说明

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

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