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

问题描述 投票:2回答:1

我在其他一个问题中提出了这个问题,但我正在研究一款专为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按钮的文本,但实际操作在实际的按钮方法中更改了几行之后(请注意,print语句只是将要放置的方法的临时占位符)后来)。

@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方法之后,它将在索引被删除后读取下一个故事。但是,如果不删除索引,我无法在故事中获得任何进展。

ios swift switch-statement ibaction
1个回答
2
投票

您不能通过选择器操作发送参数,但您可以将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属性冲突。


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

enum StoryAction {
    case attack
    case heal
    case approach
}

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

© www.soinside.com 2019 - 2024. All rights reserved.