简体   繁体   English

Swift:按下按钮时编辑Label的值

[英]Swift: Edit value of Label when pressing a button

I'm trying to learn Swift and I'm playing around with the basics of variables. 我正在尝试学习Swift,并且正在研究变量的基础知识。 I made an app where when you press a button, the value of a label changes, and while the button that adds 1 works, the one that subtracts one doesn't. 我制作了一个应用程序,当您按下一个按钮时,标签的值会更改,而加1的按钮起作用,而减一的按钮却不起作用。 If I change the first button (plusone) to subtract one, then it works, so I assume it is something to do with how the code is parsed? 如果我将第一个按钮(加号)改为一个减号,那么它将起作用,因此我认为这与代码的解析方式有关? Would you be kind enough to explain how this works? 您是否足够友善地解释其工作原理? Thanks in advance. 提前致谢。

The code is here: 代码在这里:

import UIKit

class ViewController: UIViewController {

    var value = 0
    @IBOutlet var number: UILabel!

    @IBAction func plusone(_ sender: Any) {
        value = value+1
        number.text = "\(value)"
    }

    @IBAction func plustwo(_ sender: Any) {
        value = value-1
        number.text = "\(value)"
    }

}

And the app looks like this if that matters: Screenshot 如果重要的话,该应用程序如下所示: 屏幕截图

The second button is named 'plustwo' because I haven't figured out where else I need to rename it, if I just rename it here the app displays a blank screen and nothing else. 第二个按钮被命名为“ plustwo”,因为我还没有弄清楚需要重命名的其他地方,如果我只是在此处重命名,则该应用程序将显示空白屏幕,而没有其他任何显示。

From the comments: 从评论:

I changed plustwo to -2 and it works as intended. 我将plustwo更改为-2,它按预期工作。 Pressing Add One adds one and pressing Substract One substracts one. 按“加一”加一,然后按“减一”减一。 I am not sure why. 我不知道为什么。

The behavior you describe tells me that you have your Subtract One button wired to both the plusone and plustwo functions. 您描述的行为告诉我,您的减一按钮同时连接到plusoneplustwo函数。 You can check this by control -clicking on your button in Interface Builder and checking the connections. 您可以通过以下方法进行检查:在Interface Builder中单击控制按钮,然后检查连接。 If you see TouchUpInside wired to two functions, remove one by clicking on the x to delete the connection. 如果您看到TouchUpInside连接到两个功能,请通过单击x删除一个以删除连接。

Another way you can verify the behavior is to add logging print statements to your functions such as print("in plusone") and print("in plustwo") . 验证行为的另一种方法是将日志记录print语句添加到函数中,例如print("in plusone")print("in plustwo") When you press Subtract One , if you see both messages in the console then you know you have your button connected to both @IBAction s. 当您按Subtract One时 ,如果您在控制台中看到两条消息,则表示您已将按钮连接到两个@IBAction

How did this happen? 这怎么发生的? This frequently happens when you copy a button in Interface Builder that already has a connection to an @IBAction . 当您在Interface Builder中复制一个已经与@IBAction连接的按钮时,经常会发生这种情况。 Connecting it to a new @IBAction doesn't replace the first one, it just connects to both of them and calls both when the button is pressed. 将其连接到新的@IBAction并不能替代第一个@IBAction ,它只是连接到两个@IBAction并在按下按钮时调用两者。

The second button is named 'plustwo' because I haven't figured out where else I need to rename it, if I just rename it here the app displays a blank screen and nothing else. 第二个按钮被命名为“ plustwo”,因为我还没有弄清楚需要重命名的其他地方,如果我只是在此处重命名,则该应用程序将显示空白屏幕,而没有其他任何显示。

To rename the function, you need to change the name of the function and remove and reconnect the connection in Interface Builder . 要重命名该功能,您需要更改该功能的名称, Interface Builder中删除并重新连接。 As described above, control -click on the button in Interface Builder and remove the connection to the old function name, then connect the button to the new function. 如上所述,在Interface Builder中 ,按Control键并单击按钮,并删除与旧功能名称的连接,然后将按钮连接至新功能。

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

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