- Xcode 9 / Swift 4 (Development Environment)
- Open Xcode
- Select "Create a new Xcode project"
- Select "Single View Application" and then tap "Next"
- Fill "Product name" and then tap "Next"
- Select the place for saving your project and then tap "Create"
We need 2 icons. Please download watch and trophy icon from the links. You can import them to the project.
You complete project setup. Good job! Now we can work on front side. Please select Main.storyboard
. This file help us design the app a lot. Let's work on this.
-
3-1. Let's add Navigation
- We need a navigation for the screen transitions
- To add navigation,
Editor->Embed In->Navigation Controller
so that you can add navigation to the root
-
3-2. Let's add image view
- Select
UIImageView
component from the utility area. Drag & drop to the board.
- Select
-
3-3. Resize it
- You can tap the edge of the
UIImageView
so that you can resize it. Or you can sepcify the size in the utility area.
- You can tap the edge of the
-
3-4. Set Autoresizing
- We need to adjust to all device size from iPhone to iPad. Easiest way is setting autoresizing.
-
3-5. Add
UIButton
exactory same way adding image view -
3-6. Add
UIViewController
- We need to 2 more screens which are showing
Quiz
andResult
. - Select
UIViewController
component from utility area. - Drag & drop to the board
- We need to 2 more screens which are showing
-
3-7. Design quiz & result Screens
- Youe storyboard may looks like this? Great!
-
4-1. Add
UIViewController
class- We added 2 screens. We need 2
UIVewController
classes collesponding the screens - Add new class from menu or
command + N
- One is
QuizViewController
subclass ofUIViewController
, Another isResultViewController
.
- We added 2 screens. We need 2
-
4-2. Connect
UIViewController
class to the storyboard- We need to connect the UIViewController classes to the storyboard
- Add
class name
andstoryboard ID
.QuiziViewController
andResultViewController
both - Go to
main.storyboard
-
4-3. Create segue
- We need to connect UI components to the class. i.e. Button action should be triguered after tapped
- To connect components to class, let's create segue.
control + drag in storyboard
to create a segue- We can see 2 type of segue.
Outlet
andAction
Outlet
is the component itself.Action
is action trigger
You complete the UI. Great job! Last section is coding part for adding logic.
- UIViewContoller
import UIKit
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tappedStart(_ sender: Any) {
let next = storyboard!.instantiateViewController(withIdentifier: "QuizViewController")
navigationController?.pushViewController(next, animated: true)
}
}
- QuizViewContoller
import UIKit
class QuizViewController: UIViewController {
let quiz = QuizManager.shared.getQuiz()
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var coiceButton1: UIButton!
@IBOutlet weak var coiceButton2: UIButton!
@IBOutlet weak var coiceButton3: UIButton!
@IBOutlet weak var coiceButton4: UIButton!
@IBOutlet weak var incollectLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
incollectLabel.isHidden = true
navigationItem.hidesBackButton = true
questionLabel.text = quiz["quiz"]
coiceButton1.setTitle(quiz["choice_1"], for: .normal)
coiceButton2.setTitle(quiz["choice_2"], for: .normal)
coiceButton3.setTitle(quiz["choice_3"], for: .normal)
coiceButton4.setTitle(quiz["choice_4"], for: .normal)
}
@IBAction func tappedAnswer(_ sender: UIButton) {
if quiz["answer"] != sender.titleLabel?.text {
incollectLabel.isHidden = false
return
}
let isExisting = QuizManager.shared.existNextQuiz()
let next = isExisting ?
storyboard!.instantiateViewController(withIdentifier: "QuizViewController") :
storyboard!.instantiateViewController(withIdentifier: "ResultViewController")
navigationController?.pushViewController(next, animated: true)
}
}
- ResultViewController
class ResultViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
}
@IBAction func tappedGoBack(_ sender: Any) {
navigationController?.popToRootViewController(animated: true)
}
}
- QuizManager
import UIKit
class QuizManager: NSObject {
static var shared = QuizManager()
private override init() {} // Singleton
private var quizes = [[String: String]]()
private func setupQuizzes() {
quizes = [
[
"quiz": "1. What colour ball should normally be struck by the cue in snooker?",
"answer": "white",
"choice_1": "white",
"choice_2": "mauve",
"choice_3": "black",
"choice_4": "orange"
],
[
"quiz": "2. Which word means a signed document in support of a particular action?",
"answer": "partition",
"choice_1": "petition",
"choice_2": "position",
"choice_3": "partition",
"choice_4": "perforation"
],
[
"quiz": "3. Somebody described as 'butterfingers' would have a propensity for what?",
"answer": "being clumsy",
"choice_1": "being clumsy",
"choice_2": "playing the piano",
"choice_3": "gardening",
"choice_4": "cookery"
],
]
}
func existNextQuiz() -> Bool {
return quizes.count != 0
}
func getQuiz() -> [String: String] {
if quizes.count == 0 { setupQuizzes() }
let quiz = quizes.first
quizes.removeFirst()
return quiz!
}
}