测验应用程序:使用 NSUserDefaults 保存选定的测验问题并将其显示在另一个视图控制器中
Quiz app: Saving selected quiz-questions using NSUserDefaults and displaying it in another view controller
我现在已经坚持这个问题好几个小时了,我就是不知道该怎么做,而且我对它的看法越来越模糊,所以我需要一些帮助。所以这里是我的应用程序的骨骼希望你能帮助...
我正在构建一个测验应用程序。我有测验部分工作,也就是说我已经创建了一个结构并定义了一个问答部分。然后我在屏幕上显示问题并隐藏答案,直到用户按下显示答案 button.The 用户可以向左或向右滑动以在问题之间前进或后退。
我想合并让用户能够保存一些问题并在稍后阶段在不同的视图控制器中返回它们。单击保存按钮时,我希望保存问题并将其放入保存的视图控制器中。我希望它以问题的形式出现,这样我就可以让用户快速浏览所有已保存的问题。我试图使用 NSUserDefaults 保存。
问题视图控制器的代码:
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)
}
这是问题的代码,并向用户显示问题。现在我想在单击保存按钮时保存所选问题。我需要保存它,这样我就可以在另一个视图控制器中呈现这些保存的问题,并使用户能够快速浏览他们保存的问题。我该怎么做?
如果您只想保存和检索 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
或,
如果要保存和检索 object 的数组
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
我现在已经坚持这个问题好几个小时了,我就是不知道该怎么做,而且我对它的看法越来越模糊,所以我需要一些帮助。所以这里是我的应用程序的骨骼希望你能帮助...
我正在构建一个测验应用程序。我有测验部分工作,也就是说我已经创建了一个结构并定义了一个问答部分。然后我在屏幕上显示问题并隐藏答案,直到用户按下显示答案 button.The 用户可以向左或向右滑动以在问题之间前进或后退。
我想合并让用户能够保存一些问题并在稍后阶段在不同的视图控制器中返回它们。单击保存按钮时,我希望保存问题并将其放入保存的视图控制器中。我希望它以问题的形式出现,这样我就可以让用户快速浏览所有已保存的问题。我试图使用 NSUserDefaults 保存。
问题视图控制器的代码:
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)
}
这是问题的代码,并向用户显示问题。现在我想在单击保存按钮时保存所选问题。我需要保存它,这样我就可以在另一个视图控制器中呈现这些保存的问题,并使用户能够快速浏览他们保存的问题。我该怎么做?
如果您只想保存和检索 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
或,
如果要保存和检索 object 的数组
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