これは何時間も何時間もこんなことに出会ってしまったので、どうやってそれを行うのか分かりません。それで私はいくつかの助けが必要です。だから私のアプリの骨はあなたが助けることを願っています...クイズアプリ:NSUserDefaultsを使用して選択したクイズ質問を保存し、別のビューコントローラーに表示する
私はクイズアプリを構築しています。私はクイズパートが働いています。つまり、構造を作成し、質問と回答セクションを定義しました。私は画面上に質問を表示し、ユーザーが解答解答ボタンを押すまで解答を隠す。ユーザーは左右にスワイプして質問の間を前後に移動することができる。
私は、ユーザーがいくつかの質問を保存し、後で別のビューコントローラーでそれらに戻ってくることを可能にすることを組み込みたいと考えています。保存ボタンをクリックすると、質問が保存され、保存されたビューコントローラに配置されます。私はそれを質問の形で欲しがるので、ユーザーが保存されたすべての質問を聞かせることができます。私は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)
}
質問のためのコードと、ユーザーに質問を表示する次のとおりです。
質問のコードは、コントローラを表示します。今すぐ保存ボタンをクリックすると、選択した質問を保存します。私はこれを保存する必要があるので、これらの保存された質問を別のビューコントローラーに表示して、保存された質問をフリックすることができます。どうすればいいのですか?
可能なダブ[NSCodingでUserDefaultsにカスタムSWIFTクラスを保存する](http://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults) – Emptyless
注:すべての変数をcamelCasedで記述する小文字の最初の文字で始まります。プロトコル/クラス名を書くキャメル大文字の最初の文字で – muescha
https://github.com/raywenderlich/swift-style-guide#naming – muescha