gpt4 book ai didi

swift - 测验应用程序 : Saving selected quiz-questions using NSUserDefaults and displaying it in another view controller

转载 作者:行者123 更新时间:2023-11-28 06:26:14 24 4
gpt4 key购买 nike

我现在已经坚持这个问题好几个小时了,我只是不知道该怎么做,而且我对它的看法越来越狭隘,所以我需要一些帮助。所以这里是我的应用程序的骨骼希望你能帮助...

我正在构建一个测验应用程序。我有测验部分工作,也就是说我已经创建了一个结构并定义了一个问答部分。然后我在屏幕上显示问题并隐藏答案,直到用户按下显示答案按钮。用户可以向左或向右滑动以在问题之间前进或后退。

我想合并让用户能够保存一些问题并在稍后阶段在不同的 View Controller 中返回它们。单击保存按钮时,我希望保存问题并将其放入保存的 View Controller 中。我希望它以问题的形式出现,这样我就可以让用户快速浏览所有已保存的问题。我试图使用 NSUserDefaults 保存。

问题 View Controller 的代码:

import Foundation
import UIKit

struct Question {
var Question : String!
var Answers : String!
}

class Defence: UIViewController {


@IBOutlet weak var labelForQuestion: UILabel!
@IBOutlet weak var textBoxForAnswer: UITextView!

var QNumber : Int = 0

override func viewDidLoad() {

//hiding answer
textBoxForAnswer.hidden = true

//components for swiping left
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondLeft:")
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)

//components for swiping Right
let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondRight:")
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)




Questions = [

Question(Question: "what colour is the sky", Answers: "blue"),

Question(Question: "what colour is the grass", Answers: "green",

Question(Question: "what colour is the sea", Answers: "blue",

Question(Question: "what is 1 plus 1", Answers: "2"),

Question(Question: "what is 2 plus 2", Answers: "4"),

Question(Question: "what is 3 plus 3", Answers: "6"),

]

pickQuestion()
}



func pickQuestion() {

if Questions.count > 0 {

Num.text = String(QNumber + 1)

labelForQuestion.text = Questions[QNumber].Question
textBoxForAnswer.text = Questions[QNumber].Answers

}

}

//Action for left swipe
func respondLeft(gesture: UIGestureRecognizer) {
if QNumber == (Questions.count - 1) {
//if last question do nothing so it doesnt go out of bounds
} else {
QNumber++;
pickQuestion()
}
}


//Action for Right Swipe
func respondRight(gesture: UIGestureRecognizer) {
if QNumber == 0 {
//if last question do nothing so it doesnt go out of bounds
} else {
QNumber--;
pickQuestion()
}
}


@IBAction func showAnswer(sender: AnyObject) {
textBoxForAnswer.hidden = false
}


@IBAction func save(sender: AnyObject) {
****Have tried many things here with NSUserDefaults and appending to a dictionary so I could see the saved question in the other view controllers. this is where I need help****
}


@IBAction func sections(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}

这是问题和向用户显示问题的代码。现在我想在单击保存按钮时保存所选问题。我需要保存它,这样我就可以在另一个 View Controller 中呈现这些保存的问题,并使用户能够快速浏览他们保存的问题。我该怎么做?

最佳答案

如果你只想保存和检索string类型:-

var Question: String!

//save questions from here {func save() }
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(questions, forKey: "key_questions")

//retrieve questions from another view controller
let defaults = NSUserDefaults.standardUserDefaults()
let quest = defaults.stringForKey("key_questions")
print("quest") //questions saved before from first view controller

或者,

如果你想保存和获取一个对象的数组

var Question: [[String:AnyObject]]!

//save questions from here {func save() }
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(questions, forKey: "key_questions")

//retrieve questions from another view controller
let defaults = NSUserDefaults.standardUserDefaults()
let quest = defaults.objectForKey("key_questions") as? [[String:AnyObject]]
print("quest") //questions saved before from first view controller

关于swift - 测验应用程序 : Saving selected quiz-questions using NSUserDefaults and displaying it in another view controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41797116/

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