gpt4 book ai didi

ios - 有两个选择的阵列让我只有一半

转载 作者:行者123 更新时间:2023-12-01 21:54:28 25 4
gpt4 key购买 nike

我试图使这个故事中用户有两种选择。
现在它只工作一半,我只是想不出为什么它没有改变按钮上的选择。
基本上,它可以工作到第二个if语句。

let startOfStory = [
Story(title: "You see a fork in the road", choice1: "Turn Left", choice2: "Turn Right"),
Story(title: "You see a tiger", choice1: "Shout for help", choice2: "Play Dead"),
Story(title: "You find a Treasure Chest", choice1: "Open it", choice2: "Check for Traps"),
]

var storyNumber = 0

override func viewDidLoad() {
super.viewDidLoad()

updateUI()
}

@IBAction func choiceMade(_ sender: UIButton) {

let userAnswer = sender.currentTitle

updateUI()

if userAnswer == startOfStory[0].choice1 {
storyLabel.text = startOfStory[1].title
} else if userAnswer == startOfStory[0].choice2 {
storyLabel.text = startOfStory[2].title
}

if userAnswer == startOfStory[1].choice1 {
storyLabel.text = startOfStory[0].title
} else if userAnswer == startOfStory[2].choice1 {
storyLabel.text = startOfStory[0].title
}
}

func updateUI() {
storyLabel.text = startOfStory[storyNumber].title
choice1Button.setTitle(startOfStory[storyNumber].choice1, for: .normal)
choice2Button.setTitle(startOfStory[storyNumber].choice2, for: .normal)
}

最佳答案

您的storyNumber值固定为0,因为这是它的定义,在我能看到的任何地方都不会更新。

您应在做出每个决定后都更新storyNumber以确保故事进展,并应在updateUI()函数之后调用if函数:

@IBAction func choiceMade(_ sender: UIButton) {

let userAnswer = sender.currentTitle

if userAnswer == startOfStory[0].choice1 {
storyLabel.text = startOfStory[1].title
storyNumber = 1
} else if userAnswer == startOfStory[0].choice2 {
storyLabel.text = startOfStory[2].title
storyNumber = 2
}

if userAnswer == startOfStory[1].choice1 {
storyLabel.text = startOfStory[0].title
storyNumber = 0
} else if userAnswer == startOfStory[2].choice1 {
storyLabel.text = startOfStory[0].title
storyNumber = 0
}

updateUI()
}

关于ios - 有两个选择的阵列让我只有一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61394661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com