我现在已经坚持了好几个小时,我只是无法弄清楚如何做到这一点,我正在接受隧道视野,所以我需要一些帮助。所以这是我的应用程序的骨头希望你可以帮助...
我正在构建测验应用。我有测验部分工作,即我创建了一个结构并定义了一个问答部分。然后我在屏幕上显示问题并隐藏答案,直到用户按下显示答案按钮。用户可以向左或向右滑动以在问题之间前进或后退。
我希望合并让用户能够保存一些问题,并在稍后阶段在不同的视图控制器中回复它们。单击保存按钮时,我希望保存问题并将其放入已保存的视图控制器中。我希望它以问题的形式出现,这样我就可以让用户轻松浏览所有已保存的问题。我试图使用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)
}
这是问题的代码并向用户显示问题。现在,我想在单击保存按钮时保存所选问题。我需要这个保存,所以我可以在另一个视图控制器中显示这些保存的问题,并使用户可以浏览他们保存的问题。我该怎么做?
答案 0 :(得分:1)
如果您只想保存并检索字符串,请输入: -
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
<强> OR, 强>
如果要保存和检索对象
的数组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