Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Last active July 17, 2018 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topherPedersen/be6c309ff9cdd7e6e7fc61b78b549ebf to your computer and use it in GitHub Desktop.
Save topherPedersen/be6c309ff9cdd7e6e7fc61b78b549ebf to your computer and use it in GitHub Desktop.
Magic 8 Ball Tutorial in Swift
// NOTE: This Magic 8 Ball Tutorial in Swift is for my personal reference
// teaching students at theCoderSchool in Flower Mound, TX
// This isn't a full blown application, just two swift files
// combined into one github gist for reference when assisting
// students with their "Magic 8 Ball" assignment. This app consists
// of one view containing a label, a text field, and a button.
// Users enter their question into the text field and then press
// the button to submit their question. The app then answers the user
// using a simple UIAlertController (alert/dialog popup). Students
// need to connect the button from their main storyboard to the
// ViewController using an IBAction. See the ViewController and Model
// swift files below:
//
// ViewController.swift
// Mystic 9 Ball
//
// Created by Christopher Pedersen on 7/2/18.
// Copyright © 2018 Christopher Pedersen. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.questionTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Hide Keyboard Code by "The Swift Guy" and is from the public domain
// REFERENCE: https://www.youtube.com/watch?v=l-Uup2lKw1Y
// Hide Keyboard When User Touches The Screen
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true) // hide keyboard when user touches screen
}
// Hide Keyboard When User Presses Return Key on Keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
questionTextField.resignFirstResponder()
return(true)
}
var mysticModel = MysticModel()
func displayFortune(using fortune: String) {
// REFERENCE (UIAlertViewController): https://developer.apple.com/documentation/uikit/uialertcontroller
let alert = UIAlertController(title: "Mystic 9 Ball says...", message: fortune, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
}
@IBOutlet weak var questionTextField: UITextField!
@IBAction func onAskButtonClick(_ sender: Any) {
let randomNumber = Int(arc4random_uniform(2))
self.displayFortune(using: mysticModel.fortune[randomNumber])
questionTextField.text = "" // clear text field so user can ask a new question
}
}
//
// MysticModel.swift
// Mystic 9 Ball
//
// Created by Christopher Pedersen on 7/2/18.
// Copyright © 2018 Christopher Pedersen. All rights reserved.
//
import Foundation
class MysticModel {
var fortune: [String] = []
init() {
fortune.append("Yes!")
fortune.append("No.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment