I have been stuck on this now for hours and hours, I just can't figure out how to do it and I'm getting tunnel vision with it so I need some help. So here are the bones of my app hope you can help...
I am building a Quiz app. I have the Quiz part working, that is I have created a structure and defined a question and answer section. I then display the questions on screen and hide the answer until the user pressed the reveal answer button.The users can swipe left or right to go forward or back between the questions.
I want to incorporate letting the user be able to save some questions and come back to them in a different view controller at a later stage. When the save button is clicked I want the question to be saved and placed into the saved view controller. I want it in the form of a question so I can let the user flick through all their saved questions. I was trying to save using NSUserDefaults.
The code for the Questions 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)
}
That is the code for the Questions and displaying the questions to the users. Now I want to save the selected question when the save button is clicked. I need this saved so I can present these saved questions in another view controller and enabled the user to flick through their saved Questions. How do I do it?