如何正确使用 swift 语句更改 IBAction 按钮的操作?

How can I change the action of an IBAction button using a swift statement correctly?

我在另一个问题中提出了这个问题,但我正在开发一款专为 IOS 设计的基于文本的冒险游戏。

我正在做的事情之一是在特定情况下为按钮提供多种不同的功能。我在其他一些帖子中读到,我可以通过使用 swift 语句来实现这一点,并且我在更改操作方面取得了一定的成功,但这不是正确的操作。

如下图所示,我的大部分故事以及给玩家的选项都存储在一个结构中,可以使用 PickStory 方法进行切换。

func mainStory()
{
    Storys =
        [Story(Story: "You are walking along a dirt path and come to a cross roads. You see a small shack just off the trail. What do you want to do?", Action: "Heal", North: true, South: true, East: false, West: false, storyProg: true, resultN: "You walk north and see a gate in the distance.", resultS: "Their is nothing to turn back for.", resultE: "", resultW: ""),

         Story(Story: "You see a small gate at the end of the dirt path, and their is a gaurd standing infront of the gate.", Action: "Approach", North: true, South: true, East: true, West: true, storyProg: false, resultN: "", resultS: "", resultE: "", resultW: ""),

         Story(Story: "You see a small well in the middle of town.", Action: "Attack", North: true, South: true, East: true, West: true, storyProg: false, resultN: "", resultS: "", resultE: "", resultW: "")]


    PickStory()
}

func PickStory()
{
    if Storys.count > 0
    {
        storyNum = 0
        storyLabel.text = Storys[storyNum].Story
        actionButton.setTitle(Storys[storyNum].Action, for: UIControl.State.normal)

        adjustButtons(North: Storys[storyNum].North,
                      South: Storys[storyNum].South,
                      East: Storys[storyNum].East,
                      West: Storys[storyNum].West)

        Storys.remove(at: storyNum)
    }
    else
    {
        NSLog("Done!")
    }
}

现在,虽然在 PickStory 方法中创建了 Action 按钮的文本,但在几行之后的实际按钮方法中更改了实际操作(请注意,打印语句只是临时占位符稍后将放置的方法)。

@IBAction func actionButton(_ sender: Any)
{
    switch Storys[storyNum].Action
    {
        case "Attack":
            print("Attacking")
            break
        case "Heal":
            print("Healing")
            break
        case "Approach":
            print("Approaching")
            break
    default:
        break
    }
}

总结一下问题,文字会变成正确的动作,但实际的动作不会变。

我最初的猜测是因为actionButton来得晚一些,在PickStory方法之后,它会在索引被移除后读取下一个故事。但是,如果不删除索引,我无法在故事中取得任何进展。

您不能通过选择器操作发送参数,但您可以子类化 UIButton 以添加自定义 属性,可以是任何内容。

import UIKit

class CustomButton: UIButton {
    var storyAction: String

    override init(frame: CGRect) {
        self.storyAction = ""
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        self.storyAction = ""
        super.init(coder: aDecoder)
    }
}

你可以在设置标题的时候设置这个属性:

let action = Storys[storyNum].action // Customary to use lower case for instance properties and upper case for classes
actionButton.setTitle(action, for: UIControl.State.normal)
actionButton.storyAction = action

可以在switch语句中查看

@IBAction func actionButton(_ sender: CustomButton)
{
    switch sender.storyAction
    {
        case "Attack":
            print("Attacking")
            break
        case "Heal":
            print("Healing")
            break
        case "Approach":
            print("Approaching")
            break
    default:
        break
    }
}

注意我把属性叫做storyAction是为了不和按钮的pre-existing冲突action属性.


对所有 storyAction 类型使用 enum 而不是字符串可能 更安全。这样可以更轻松地确保没有拼写错误导致问题!

enum StoryAction {
    case attack
    case heal
    case approach
}

这可以根据需要进行扩展。使用 case .attack

可以很容易地检查 switch 语句