首页 > 解决方案 > Swift 中 UIButton 的不常见行为

问题描述

我正在创建一个简单的故事游戏,您可以在其中描述情况以及 2 个动作。接下来的情况取决于您的选择。

数据存储类 ''' 类 StoryData {

var storyText: String
var answerA: String
var answerB: String
var nextStoryA: StoryData?
var nextStoryB: StoryData?

init(text: String, answerAText: String, answerBText: String) {
    storyText = text
    answerA = answerAText
    answerB = answerBText
}

} '''

程序 ''' 类 ViewController: UIViewController {

var Story: [StoryData] = []
var currentStory: StoryData = StoryData(text: "", answerAText: "", answerBText: "")

// UI Elements linked to the storyboard
@IBOutlet weak var topButton: UIButton!         // Has TAG = 1
@IBOutlet weak var bottomButton: UIButton!      // Has TAG = 2
@IBOutlet weak var storyTextView: UILabel!




override func viewDidLoad() {
    super.viewDidLoad()
    // Story data storing
    Story.append(StoryData(text: "Your car has blown a tire on a winding road in the middle of nowhere with no cell phone reception. You decide to hitchhike. A rusty pickup truck rumbles to a stop next to you. A man with a wide brimmed hat with soulless eyes opens the passenger door for you and asks: \"Need a ride, boy?\".", answerAText: "I\'ll hop in. Thanks for the help!", answerBText: "Better ask him if he\'s a murderer first."))

    Story.append(StoryData(text: "He nods slowly, unphased by the question.", answerAText: "At least he\'s honest. I\'ll climb in.", answerBText: "Wait, I know how to change a tire."))

    Story.append(StoryData(text: "As you begin to drive, the stranger starts talking about his relationship with his mother. He gets angrier and angrier by the minute. He asks you to open the glovebox. Inside you find a bloody knife, two severed fingers, and a cassette tape of Elton John. He reaches for the glove box.", answerAText: "I love Elton John! Hand him the cassette tape.", answerBText: "It\'s him or me! You take the knife and stab him."))

    Story.append(StoryData(text: "What? Such a cop out! Did you know traffic accidents are the second leading cause of accidental death for most adult age groups?", answerAText: "", answerBText: ""))

    Story.append(StoryData(text: "As you smash through the guardrail and careen towards the jagged rocks below you reflect on the dubious wisdom of stabbing someone while they are driving a car you are in.", answerAText: "", answerBText: ""))

    Story.append(StoryData(text: "You bond with the murderer while crooning verses of \"Can you feel the love tonight\". He drops you off at the next town. Before you go he asks you if you know any good places to dump bodies. You reply: \"Try the pier.\"", answerAText: "", answerBText: ""))


    // Story tree creating
    Story[0].nextStoryA = Story[2]
    Story[0].nextStoryB = Story[1]
    Story[1].nextStoryA = Story[2]
    Story[1].nextStoryB = Story[3]
    Story[2].nextStoryA = Story[5]
    Story[2].nextStoryB = Story[4]

    currentStory = Story[0]

    updateUI()

}


// User presses one of the buttons
@IBAction func buttonPressed(_ sender: UIButton) {

    if sender.tag == 1{

        currentStory = currentStory.nextStoryA ?? currentStory

    }
    else if sender.tag == 2{

        currentStory = currentStory.nextStoryB ?? currentStory

    }

    updateUI()

}

func updateUI() {

    storyTextView.text = currentStory.storyText
    topButton.setTitle(currentStory.answerA, for: .normal)
    bottomButton.setTitle(currentStory.answerB, for: .normal)

    if topButton.currentTitle == ""{
        topButton.frame.size.height = 0
        topButton.frame.size.width = 0
    }
    if bottomButton.currentTitle == ""{
        bottomButton.frame.size.height = 0
        bottomButton.frame.size.width = 0
    }

}

} '''

当谈到游戏结束按钮不会消失。当标题为空值时,它们必须消失。

标签: iosswiftuibutton

解决方案


无需检查任何内容中的空文本buttons并更新frames,您可以简单地根据您的模型隐藏/显示buttons

只需updateUI()使用以下 2 行更新方法,即

func updateUI() {
    storyTextView.text = currentStory.storyText
    topButton.setTitle(currentStory.answerA, for: .normal)
    bottomButton.setTitle(currentStory.answerB, for: .normal)

    //Add below 2 lines instead
    topButton.isHidden = currentStory.answerA.isEmpty
    bottomButton.isHidden = currentStory.answerB.isEmpty
}

如果您仍然遇到任何问题,请告诉我。


推荐阅读