ios 如何在 Swift 中为多个按钮使用一个 IBAction?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37870701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:25:38  来源:igfitidea点击:

How to use one IBAction for multiple buttons in Swift?

iosiphoneswiftuibutton

提问by SwiftyJD

I have multiple buttons each one with the ability to switch the language of the app. Instead of having to create multiple IBActions for each button is there a way to have them all connected to one IBAction and change the language based on the button pressed? I'm thinking a switch statement would be good to use in this situation but not exactly sure how to set it up.

我有多个按钮,每个按钮都可以切换应用程序的语言。不必为每个按钮创建多个 IBAction,有没有办法让它们全部连接到一个 IBAction 并根据按下的按钮更改语言?我认为在这种情况下使用 switch 语句会很好,但不确定如何设置它。

回答by Code Different

In Interface Builder, select the Attributes Inspector and set the Tag for each button with a unique number, then you can do something like this:

在 Interface Builder 中,选择 Attributes Inspector 并为每个按钮设置一个唯一编号的 Tag,然后您可以执行以下操作:

@IBAction changeLanguage(sender: AnyObject) {
    guard let button = sender as? UIButton else {
        return
    }

    switch button.tag {
    case 1:
        // Change to English
    case 2:
        // Change to Spanish
    case 3:
        // Change to French, etc
    default:
        print("Unknown language")
        return
    }
}

To connect the action to multiple buttons:in Interface Builder, right-click ViewController in the view hierarchy, then left-click to drag the action connection to each button.

将动作连接到多个按钮:在界面生成器中,右键单击视图层次结构中的 ViewController,然后左键单击将动作连接拖到每个按钮上。

回答by Hayden McCabe

Yes, a switch statement is the way to go here. For a UIButton, you link it to a selector that is called when the user interacts with the button, generally the TouchUpInside event. The addTarget method, and valid selector signatures (apple.com)Of these, you want to use a method in the format @IBAction func doSomething(sender: UIButton)or @IBAction func doSomething(sender: UIButton, forEvent event: UIEvent), so that a reference to the button that triggered the event is passed to the selector.

是的, switch 语句是这里的方法。对于 UIButton,您将其链接到用户与按钮交互时调用的选择器,通常是 TouchUpInside 事件。addTarget 方法和有效的选择器签名 (apple.com)其中,您希望使用格式为@IBAction func doSomething(sender: UIButton)or的方法@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent),以便将触发事件的按钮的引用传递给选择器。

In your ViewController code, you'll have references to your UIButtons (possibly in a storyboard, or created manually.) Let's say you have

在您的 ViewController 代码中,您将引用您的 UIButtons(可能在情节提要中,或手动创建。)假设您有

@IBOutlet weak var frenchButton: UIButton!
@IBOutlet weak var spanishButton: UIButton!
@IBOutlet weak var englishButton: UIButton!

You would connect all of them to the same method, and branch the logic based on which one was the sender. e.g.:

您可以将所有这些连接到相同的方法,并根据哪个是发件人来分支逻辑。例如:

@IBAction func changeLanguage(sender: UIButton) {
    switch sender {
    case frenchButton:
        // Change Language to French
        print ("C'est si bon")
    case spanishButton:
        // or Spanish
        print ("Muy Bueno")
    case englishButton:
        // or English
        print ("It's pretty cool")
    default:
        break

    }

}

Note: Case statements in Swift must be exhaustive, so you have to include a default case, even though it should never be called.

注意:Swift 中的 Case 语句必须是详尽无遗的,因此您必须包含一个默认 case,即使它不应该被调用。

回答by Edward Anthony

Do not set tag if you have reference to the button.

如果您有对按钮的引用,请不要设置标签。

You can just compare the reference instead of tags. This way, you won't introduce a new bug, because unlike a tag that you type yourself, reference is created by compiler automatically.

您可以只比较参考而不是标签。这样,您就不会引入新的错误,因为与您自己键入的标签不同,引用是由编译器自动创建的。

@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!
@IBOutlet weak var thirdButton: UIButton!

@IBAction changeLanguage(sender: UIButton) {
    if sender == firstButton {

    } else if sender == secondButton {

    } else if sender == thirdButton {

    }
}