Quiz App
This is intended to be done in 1 or 2 hours in Girls Who Code College Loop Club iOS Development Workshop. To learn more, please read Apple's Documentation that will explain a lot more. This is just for fun to build your first iOS app. 
A version of the finished app can be found here
Create the project
- Open Xcode and create a new project and select "Single View App" for your template. Name your project “Quiz App”, or about whatever topic you want.
- Uncheck Use Core Data, UI Tests, etc. Then check the location you are saving your project and save it somewhere you can easily find it like your thumb drive or the desktop of your computer.
Create a second view
- Select the Main.storyboard file in the navigator
- Shift-Command-L search for view controller and add to main.storyboard
Add the UI elements to your first and second views
- Shift-Command-L to add element
- For the quiz screen add
least two labels
four buttons
(One label will show the question for the quiz, one label will show the quiz progress, and the buttons will display the answer choices.)
- For the results screen you should at least include 
two labels
(One label will give the user a message when they complete the quiz depending on how they did and the second label will display how many answers they got correct during the quiz. You may also want to add a "Try Again" button to allow the user to retake the quiz.)-
- Add a vertical stack view for both
Drag all buttons and labels into the vertical stack view
Click on each vertical stack view and make distribution “fill equally”

Connect the UI
- Click on View Controller (VC) with 4 button and change the class to "QuestionsViewController"
Click on the other VC change the class to "ResultsViewController"
- Rename ViewController.swift to QuestionsViewController.swift
- In QuestionsViewController.swift, change every instance of ViewController to QuestionsViewController
- add a new VC called ResultsViewController.swift (New File -> Cocoa Touch Class -> name it and it should be a UIViewController)
- Clean the project (Command-Shift-K) and close the xcode and reopen.
- Open the QuestionViewController.swift on one side and QuestionViewController storyboard on the other side
-- control click on the label and buttons and drag onto the QuestionViewControl.swift and make it an "IBOutlet" named like 
- questionLabel for the first label
- answerOneButton for the first button (answerTwoButton etc for the rest)
- currentResultsLabel for the last label
Then for each of the buttons, Control-drag each into QuestionViewControl.swift again and select "IBAction" and name them something like:
- answerOneSubmit, answerTwoSubmit, etc
For the results screen, create the IBOutlets for the labels:
- resultsLabel for the first label
- resultsOutOfWhatLabel for the second
- If there's a retry button, "retryButton"
Create a Segue
- In the storyboard, Control-Drag from little yellow square at the top of the QuestionViewController to the ResultsViewController (Under "Manual Segue" click show)
- Click on this segue and make the identifier (found on the right pane), "showResultsSegue"
- If you want a retry button, control-drag it into the QuestionViewController and the (Under "Manual Segue" click show)

Quick Test
- Run your project by clicking the play in the top-Left corner. If any issues arrise, let me know and I'll come by to help!
Making it work
In QuestionViewController.swift, create a struct 
''' 
struct Question { 
let question: String 
let answers: [String] 
let correctAnswer: Int 
}
'''
Let’s break that struct down:
question will store the question being asked.
answers will store the four possible answer choices the user will see.
correctAnswer will store the index of the correct answer. Index means the location of the answer choice in the array. In an array, the first value in the array is at index position 0, the second value is at index position 1, the third value is at index position 2, and so on.
create a struct like questions:
- Create a variable named currentQuestion by: 
var currentQuestion: Question?
- Create a variable that keeps track of the current question: 
var currentQuestionPosition: Int = 0
- Create a variable that keeps track of the number of questions the person got right:
var numberRight: Int = 0
Then add something like,
Getting buttons up and running
- Inside each IBAction, add 
checkAnswer(choiceNo: 0)
It's choice number should be it's corresponding button number minus one (ie 
checkAnswer(choiceNo: 0) for answerOneSubmit, checkAnswer(choiceNo: 1) for answerTwoSubmit etc)
Update the ViewDidLoad
In QuestionViewController.swift, after "super.viewDidLoad()" add 
currentQuestion = questions[0]
setQuestion()
Try running here to debug any issues before anything else....
Setting up the ResultViewController.swift
Let's program what happens in the segue, we want this to happen when all the questions are finished. A segue looks like, (this will initially cause an error) 
in the resultViewController.swift, under IBOutlets, create two variables:
    var numberCorrect: Int = 0
    var totalQuestions: Int = 0
In resultViewController.swift's viewDidLoad, at the end add something like
    resultsOutOfWhatLabel.text = "You got \(numberCorrect) out of \(totalQuestions)"
    resultsLabel.text = "Hey Stranger Things Fan"

you can also change the value of your retry button by adding the button as a IBOutlet and then adding this in the ViewDidLoad
retryButton.setTitle("Retry", for: .normal)
That's it!! Run it and if you run into any issues try debugging or ask me!
Back to Top