首页 > 解决方案 > 如何正确使用 swift 语句更改 IBAction 按钮的操作?

问题描述

我在其他一个问题中提出了这个问题,但我正在开发一款为 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 方法之后,它会在删除索引后读取下一个故事。但是,如果不删除索引,我无法在故事中取得任何进展。

标签: iosswiftswitch-statementibaction

解决方案


您不能通过选择器操作发送参数,但您可以将其子类化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是为了不与按钮的预先存在的action属性冲突。


对所有类型使用 an可能比字符串更安全。这将更容易确保没有拼写错误导致问题!enumstoryAction

enum StoryAction {
    case attack
    case heal
    case approach
}

这可以根据需要进行扩展。case .attack使用等检查 switch 语句很容易。


推荐阅读