UISegmentedControl 和 GUI 之间的交互逻辑在我的应用程序中不起作用
The logic of the interaction between UISegmentedControl and GUI doesn't work in my app
我尝试在问答游戏中实现的功能如下所示:
这里是SettingsViewController
里面包含了一个app的设置,用户可以在这里选择游戏的difficulity
和游戏的sequence
问题使用UISegmentedControl
.
SettingsViewController
包含用于实现此功能的这些方法:
@IBAction func didChooseDifficulty(_ sender: UISegmentedControl) {
difficulityDelegate?.didChooseDifficulity(
difficulity: selectedDifficulty)
UserDefaults.standard.set(
sender.selectedSegmentIndex,
forKey: "chosenDifficulity")
}
private var selectedDifficulty: Difficulty {
switch self.difficultyControl.selectedSegmentIndex {
case 0: return .easy
case 1: return .hard
case 2: return .insane
default: return .hard
}
}
@IBAction func didChooseSequence(_ sender: UISegmentedControl) {
sequenceDelegate?.didChooseSequence(
sequence: selectedSequence)
UserDefaults.standard.set(
sender.selectedSegmentIndex,
forKey: "chosenSequence")
}
private var selectedSequence: Sequence {
switch self.sequenceControl.selectedSegmentIndex {
case 0: return .sequentally
case 1: return .shuffled
default: return .sequentally
}
}
override func viewDidLoad() {
super.viewDidLoad()
setupQuestionsSequence()
setupDifficulty()
}
func setupQuestionsSequence() {
if let sequence = UserDefaults.standard.value(
forKey: "chosenSequence") {
let selectedIndex = sequence as! Int
sequenceControl.selectedSegmentIndex = selectedIndex
}
}
func setupDifficulty() {
if let difficulity = UserDefaults.standard.value(
forKey: "chosenDifficulity") {
let selectedIndex = difficulity as! Int
difficultyControl.selectedSegmentIndex = selectedIndex
}
}
我在这里使用 DifficulityDelegate
和 ChooseQuestionsSequenceDelegate
协议:
protocol DifficulityDelegate: AnyObject {
func didChooseDifficulity(
difficulity: Difficulty)
}
protocol ChooseQuestionsSequenceDelegate: AnyObject {
func didChooseSequence(
sequence: Sequence)
}
这里还有GameDelegate
协议,我用的游戏主要协议:
protocol GameDelegate: AnyObject {
func didEndGame(
difficulty: Difficulty,
withScore score: Int,
name: String,
removeTwoUsed: Bool,
callFriendUsed: Bool,
audienceHelpUsed: Bool)
}
这里是 GameSession
class 用于游戏的主要逻辑:
class GameSession {
var questionNumber: Int = 0
var callFriendUsed = false
var audienceHelpUsed = false
var removeTwoUsed = false
var difficulty: Difficulty = .hard
var sequence: Sequence = .sequentally
}
这是 Question
结构,其中有问题:
struct Question: Codable {
let question: String
let answers: [String]
let rightAnswer: Int
let difficulty: Difficulty
}
这里是 GameViewController
,游戏本身的主控制器。
我需要了解为什么当我更改 UISegmentedControl
状态时没有任何反应。游戏没有任何变化difficulty
,出现问题sequence
。
这是来自 GameViewController
的一些代码:
weak var delegate: GameDelegate?
weak var sequenceDelegate: ChooseQuestionsSequenceDelegate?
weak var difficulityDelegate: DifficulityDelegate?
private var questions = [Question]()
var gameSession = GameSession()
var score = 0
var questionNumber = Observable<Int>(0)
var difficulty: Difficulty = .hard {
didSet {
gameSession.difficulty = difficulty
}
}
var sequence: Sequence = .sequentally {
didSet {
gameSession.sequence = sequence
}
}
private var chooseDifficultyStrategy: DiffilultyStrategy {
switch difficulty {
case .easy: return EasyModeStrategy()
case .hard: return HardModeStrategy()
case .insane: return InsaneModeStrategy()
}
}
private var setupSelectedSequence: SequenceStrategy {
switch sequence {
case .sequentally: return SequentiallyStrategy()
case .shuffled: return ShuffledStrategy()
}
}
func initialSetup() {
delegate = Game.instance
Game.instance.gameSession = gameSession
questions = chooseDifficultyStrategy.setupGame(
lifelineButtons: lifelineButtonsCollection)
questions = setupSelectedSequence.setupGame()
}
这可能是愚蠢的,如果有人能帮助我理解我做错了什么,我会很高兴。谢谢!
从非常广泛的角度来说,我会说 protocol-and-delegate 机制不是这里想要的。与用户偏好交互的视图控制器,GameViewController和SettingsViewController,不一定同时存在,所以它们不能相互通信。
更可能需要的是一些中央存储库,SettingsViewController 可以在其中写下用户的需求,而 GameViewController 可以读取它们。像这样保存首选项的常见位置是 UserDefaults;它的优点是它会随着时间的推移持续存在,甚至在应用程序启动之间持续存在,并且它是全球可用的(这意味着您在任何地方的任何代码都可以与之交互)。
我尝试在问答游戏中实现的功能如下所示:
这里是SettingsViewController
里面包含了一个app的设置,用户可以在这里选择游戏的difficulity
和游戏的sequence
问题使用UISegmentedControl
.
SettingsViewController
包含用于实现此功能的这些方法:
@IBAction func didChooseDifficulty(_ sender: UISegmentedControl) {
difficulityDelegate?.didChooseDifficulity(
difficulity: selectedDifficulty)
UserDefaults.standard.set(
sender.selectedSegmentIndex,
forKey: "chosenDifficulity")
}
private var selectedDifficulty: Difficulty {
switch self.difficultyControl.selectedSegmentIndex {
case 0: return .easy
case 1: return .hard
case 2: return .insane
default: return .hard
}
}
@IBAction func didChooseSequence(_ sender: UISegmentedControl) {
sequenceDelegate?.didChooseSequence(
sequence: selectedSequence)
UserDefaults.standard.set(
sender.selectedSegmentIndex,
forKey: "chosenSequence")
}
private var selectedSequence: Sequence {
switch self.sequenceControl.selectedSegmentIndex {
case 0: return .sequentally
case 1: return .shuffled
default: return .sequentally
}
}
override func viewDidLoad() {
super.viewDidLoad()
setupQuestionsSequence()
setupDifficulty()
}
func setupQuestionsSequence() {
if let sequence = UserDefaults.standard.value(
forKey: "chosenSequence") {
let selectedIndex = sequence as! Int
sequenceControl.selectedSegmentIndex = selectedIndex
}
}
func setupDifficulty() {
if let difficulity = UserDefaults.standard.value(
forKey: "chosenDifficulity") {
let selectedIndex = difficulity as! Int
difficultyControl.selectedSegmentIndex = selectedIndex
}
}
我在这里使用 DifficulityDelegate
和 ChooseQuestionsSequenceDelegate
协议:
protocol DifficulityDelegate: AnyObject {
func didChooseDifficulity(
difficulity: Difficulty)
}
protocol ChooseQuestionsSequenceDelegate: AnyObject {
func didChooseSequence(
sequence: Sequence)
}
这里还有GameDelegate
协议,我用的游戏主要协议:
protocol GameDelegate: AnyObject {
func didEndGame(
difficulty: Difficulty,
withScore score: Int,
name: String,
removeTwoUsed: Bool,
callFriendUsed: Bool,
audienceHelpUsed: Bool)
}
这里是 GameSession
class 用于游戏的主要逻辑:
class GameSession {
var questionNumber: Int = 0
var callFriendUsed = false
var audienceHelpUsed = false
var removeTwoUsed = false
var difficulty: Difficulty = .hard
var sequence: Sequence = .sequentally
}
这是 Question
结构,其中有问题:
struct Question: Codable {
let question: String
let answers: [String]
let rightAnswer: Int
let difficulty: Difficulty
}
这里是 GameViewController
,游戏本身的主控制器。
我需要了解为什么当我更改 UISegmentedControl
状态时没有任何反应。游戏没有任何变化difficulty
,出现问题sequence
。
这是来自 GameViewController
的一些代码:
weak var delegate: GameDelegate?
weak var sequenceDelegate: ChooseQuestionsSequenceDelegate?
weak var difficulityDelegate: DifficulityDelegate?
private var questions = [Question]()
var gameSession = GameSession()
var score = 0
var questionNumber = Observable<Int>(0)
var difficulty: Difficulty = .hard {
didSet {
gameSession.difficulty = difficulty
}
}
var sequence: Sequence = .sequentally {
didSet {
gameSession.sequence = sequence
}
}
private var chooseDifficultyStrategy: DiffilultyStrategy {
switch difficulty {
case .easy: return EasyModeStrategy()
case .hard: return HardModeStrategy()
case .insane: return InsaneModeStrategy()
}
}
private var setupSelectedSequence: SequenceStrategy {
switch sequence {
case .sequentally: return SequentiallyStrategy()
case .shuffled: return ShuffledStrategy()
}
}
func initialSetup() {
delegate = Game.instance
Game.instance.gameSession = gameSession
questions = chooseDifficultyStrategy.setupGame(
lifelineButtons: lifelineButtonsCollection)
questions = setupSelectedSequence.setupGame()
}
这可能是愚蠢的,如果有人能帮助我理解我做错了什么,我会很高兴。谢谢!
从非常广泛的角度来说,我会说 protocol-and-delegate 机制不是这里想要的。与用户偏好交互的视图控制器,GameViewController和SettingsViewController,不一定同时存在,所以它们不能相互通信。
更可能需要的是一些中央存储库,SettingsViewController 可以在其中写下用户的需求,而 GameViewController 可以读取它们。像这样保存首选项的常见位置是 UserDefaults;它的优点是它会随着时间的推移持续存在,甚至在应用程序启动之间持续存在,并且它是全球可用的(这意味着您在任何地方的任何代码都可以与之交互)。