Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.17 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Quizzler
  4. //
  5. //  Created by Angela Yu on 25/08/2015.
  6. //  Copyright (c) 2015 London App Brewery. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.    
  13.     //Place your instance variables here
  14.     let allQuestions = QuestionBank()
  15.     var pickedAnswer : Bool = false
  16.     var questionNumber : Int = 0
  17.    
  18.     @IBOutlet weak var questionLabel: UILabel!
  19.     @IBOutlet weak var scoreLabel: UILabel!
  20.     @IBOutlet var progressBar: UIView!
  21.     @IBOutlet weak var progressLabel: UILabel!
  22.    
  23.     override func viewDidLoad() {
  24.         super.viewDidLoad()
  25.        
  26.         let firstQuestion = allQuestions.list[0]
  27.         questionLabel.text = firstQuestion.questionText
  28.        
  29.     }
  30.  
  31.  
  32.     @IBAction func answerPressed(_ sender: AnyObject) {
  33.  
  34.         if sender.tag == 1{
  35.           pickedAnswer = true;
  36.         }
  37.         else if sender.tag == 2 {
  38.             pickedAnswer = false;
  39.         }
  40.         checkAnswer()
  41.        
  42.         questionNumber = questionNumber + 1
  43.        
  44.         nextQuestion()
  45.        
  46.     }
  47.    
  48.    
  49.     func updateUI() {
  50.      
  51.     }
  52.    
  53.  
  54.     func nextQuestion() {
  55.        
  56.         if questionNumber <= 12 {
  57.         questionLabel.text = allQuestions.list[questionNumber].questionText
  58.         } else {
  59.          
  60.             let alert = UIAlertController(title: "End of Quiz!", message: "Would you like to restart?", preferredStyle: .alert)
  61.            
  62.             let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (action: UIAlertAction) in
  63.                     self.startOver()
  64.             })
  65.            
  66.             alert.addAction(restartAction)
  67.             present(alert, animated: true, completion: nil)
  68.         }
  69.        
  70.     }
  71.    
  72.    
  73.     func checkAnswer() {
  74.        
  75.         let correctAnswer = allQuestions.list[questionNumber].answer
  76.        
  77.         if correctAnswer == pickedAnswer {
  78.             print("You got it!")
  79.         } else {
  80.             print("Wrong answer cuz :/")
  81.         }
  82.     }
  83.    
  84.    
  85.     func startOver() {
  86.         questionNumber = 0
  87.         nextQuestion()
  88.        
  89.     }
  90.    
  91.  
  92.    
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement