如何在多个按钮上使用“Touch Up Inside”?

huangapple go评论68阅读模式
英文:

How do I use Touch Up Inside for several buttons?

问题

我试图使标签的文本根据按下的按钮改变为不同的文本。例如,如果按下 `btn_1``label_1` 将显示一些文本,当按下 `btn_2` 时,它会显示其他内容。

我现在的代码是:

```swift
@IBOutlet weak var label_1 : UILabel!
@IBOutlet weak var btn_1 : UIButton!
@IBOutlet weak var btn_2 : UIButton!
@IBAction func onTapChangeText(_ sender:Any) {
    label_1.text = "Hello"
}

问题在于,无论我点击哪个按钮,它都会将文本更改为 "Hello"。但我只想在按下 btn_1 时才更改,而不是同时按下 btn_2

我尝试将它更改为:

@IBAction func onTapChangeText(_ sender:btn_1) {
    label_1.text = "Hello"
}

然而,它会报错。我也尝试过:

@IBAction func onTapChangeText(_ sender:Any) {
    if sender == btn_1 {
        label_1.text = "Hello"
    }
}

但仍然不起作用。它仍然会返回一个错误(不过这也不奇怪)。

我对 Xcode 还很陌生,但对一般编码并不陌生。请问有人能告诉我如何解决这个问题吗?

顺便说一下:所有按钮都已链接,标签也已链接,按钮链接到 ViewController 上的 Touch Up Inside 以调用 onTapChangeText()。


<details>
<summary>英文:</summary>

I am trying to make a label&#39;s text change to different text based on the buttons pressed. For example, if `btn_1` is pressed `label_1` will show some text, and when `btn_2` is pressed, it&#39;ll show something else.

So what I have right now is

@IBOutlet weak var label_1 : UILabel!
@IBOutlet weak var btn_1 : UIButton!
@IBOutlet weak var btn_2 : UIButton!
@IBAction func onTapChangeText(_ sender:Any) {
label_1.text = "Hello"
}


The problem now is that anytime I tap on any button it changes the text
 to &quot;Hello&quot;. But I only want to it change when `btn_1` is pressed, not also `btn_2`.

I tried changing it to:

@IBAction func onTapChangeText(_ sender:btn_1) {
label_1.text = "Hello"
}

However, it just calls an error. I also tried 

@IBAction func onTapChangeText(_ sender:Any) {
if sender == btn_1 {
label_1.text = "Hello"
}
}

But it still doesn&#39;t work. It returns an error instead (No surprise however)

I&#39;m new to Xcode, but not to general coding. Can someone please tell me how to fix this? 

By the way: all the buttons are linked, the label is linked, and the buttons are link to call a onTapChangeText() from the ViewController on Touch Up Inside.


</details>


# 答案1
**得分**: 2

You could use separate action methods for each button or you can fix the error and have a single method handle all the buttons. You didn't show the error, but I assume it is:

&gt; Binary operator &#39;==&#39; cannot be applied to operands of type &#39;Any&#39; and &#39;UIButton&#39;

This occurs because the default type for `sender` when an action is created in Interface Builder is `Any`. Since you know they are buttons you can change the type and should then be able to determine which button was tapped:

```Swift
@IBAction func onTapChangeText(_ sender: UIButton) {
    if sender == btn_1 {
        label_1.text = "Hello"
    } else if sender == btn_2 {
        label_1.text = "Goodbye"
    }
}

It is also possible to change the type from Any to the exact type when creating the action by changing the properties in Interface Builder (away from Mac so can't show a screenshot).

英文:

You could use separate action methods for each button or you can fix the error and have a single method handle all the buttons. You didn’t show the error, but I assume it is:

> Binary operator '==' cannot be applied to operands of type 'Any' and 'UIButton'

This occurs because the default type for sender when an action is created in Interface Builder is Any. Since you know they are buttons you can change the type and should then be able to determine which button was tapped:

@IBAction func onTapChangeText(_ sender: UIButton) {
    if sender == btn_1 {
        label_1.text = &quot;Hello&quot;
    } else if sender == btn_2 {
        label_1.text = &quot;Goodbye”
    }
}

It is also possible to change the type from Any to the exact type when creating the action by changing the properties in Interface Builder (away from Mac so can’t show a screenshot).

huangapple
  • 本文由 发表于 2023年4月10日 21:13:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定