user8703776
user8703776

Reputation:

Show text from JSON with using button

I have this JSON file with text and name: 0.json:

{
"questions" : [{"text1": "1", "text2": "1"},
               {"text1": "2", "text2": "2"},
               {"text1": "3", "text2": "3"}]
}

I print text1, text2 from this file and show it in label

My code:

struct Root : Decodable {
    let questions : [Question]
}

struct Question : Decodable {
    let text1, text2 : String
}

override func viewDidLoad() {
        super.viewDidLoad()
let url = Bundle.main.url(forResource: "0", withExtension: "json")!

        do {
            let data = try! Data(contentsOf: url)
            let result = try JSONDecoder().decode(Root.self, from: data)
            for question in result.questions {
                label2.text = "\(question.text2)"
                label1.text = "\(question.text1)"
            }

        } catch {
            print("error: ", error)
        }

    }

When app starts I want to show first line "text1": "1", "text2": "1" and if user clicked on button I want to show second line "text1": "2", "text2": "2" and after button clicked I want to show third line "text1": "3", "text2": "3" and etc. How to do it?

Update code:

{
    "questions" : [{"number": "1", "text": "1", "answer": "1"},
                   {"number": "2", "text": "2", "answer": "2"},
                   {"number": "3", "text": "3", "answer": "3"}]
}

override func viewDidLoad() {
        super.viewDidLoad()

        let url = Bundle.main.url(forResource: "0", withExtension: "json")!
        let data = try! Data(contentsOf: url)
        let result = try! JSONDecoder().decode(Root.self, from: data)
        self.questions = result.questions

        textField.delegate = self
        textField.returnKeyType = .done
        _ = textFieldShouldReturn(textField)

    }

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        let question = questions[counter]
        title = question.number
        textLabel.text = question.text
        showAnswerLabel.text = question.answer
        counter = (counter + 1) % questions.count

        if textField.text == question.answer {
            print("right")

        }

        return true
    }

Upvotes: 1

Views: 370

Answers (1)

vadian
vadian

Reputation: 285082

  • Create properties for the questions and a counter

    var questions = [Question]()
    var counter = 0
    
  • in viewDidLoad populate questions

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let url = Bundle.main.url(forResource: "0", withExtension: "json")!
        let data = try! Data(contentsOf: url)
        let result = try! JSONDecoder().decode(Root.self, from: data)
        self.questions = result.questions
     }
    
  • Create an IBAction. In the body get the question for the current counter value, assign the text values to the labels and increment the counter. If the counter exceeds the number of items in questions reset the counter to 0 (conveniently performed by the % operator). Connect the action to the button in Interface Builder

    @IBAction func push(_ sender : UIButton)
    {
        let question = questions[counter]
        label2.text = question.text2 // no String Interpolation
        label1.text = question.text1
        counter = (counter + 1) % questions.count
    }
    

Upvotes: 1

Related Questions