2017-01-22 9 views
1

나는 지금 몇 시간 동안이 일에 매달 렸습니다. 어떻게 해야할지 모르겠지만 터널 비전을 얻고 있습니다. 그래서 도움이 필요합니다. 그래서 여기에 내 애플 리케이션의 뼈가 도울 수 있기를 바랍니다 ...퀴즈 응용 프로그램 : 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) 
} 

. 이제 저장 버튼을 클릭 할 때 선택한 질문을 저장하려고합니다. 저장 한이 질문을 다른보기 컨트롤러에 표시하고 사용자가 저장된 질문을 쓸어 넘길 수 있도록 저장해야합니다. 어떻게해야합니까?

+0

가능한 이중 [사용자 지정 SWIFT 클래스를 NSCoding에 UserDefaults로 저장] (http://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults) – Emptyless

+0

메모 : 모든 변수를 camelCased로 작성하십시오. 소문자 첫 글자. 프로토콜/클래스 이름 쓰기 Camel 대문자 첫 글자로 표시 – muescha

+0

https://github.com/raywenderlich/swift-style-guide#naming – muescha

답변

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