Skip to content

Instantly share code, notes, and snippets.

@kenji272
Last active August 29, 2015 14:19
Show Gist options
  • Save kenji272/bcad35318cd1c8b3013d to your computer and use it in GitHub Desktop.
Save kenji272/bcad35318cd1c8b3013d to your computer and use it in GitHub Desktop.
【課題】Part6 スライダーを指定された位置に移動させるゲームアプリ
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var targetCountLabel: UILabel!
@IBOutlet weak var answerCountSlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// 1〜100までのランダム値を取得
let targetCount = arc4random_uniform(100) + 1
self.targetCountLabel.text = "\(targetCount)"
// スライダー値初期化
self.answerCountSlider.value = 50.0
}
@IBAction func judge(sender: AnyObject) {
let answerCount = Int(self.answerCountSlider.value)
let targetCount = (self.targetCountLabel.text! as NSString).integerValue
// アラート用のメッセージを設定
var message = answerCount == targetCount ? "あたり!" : "はずれ!"
message += "\r\nあなたの値: \(answerCount)"
// アラート表示
self.viewAlert(message)
}
// 正誤判定のアラートを表示させるメソッド
func viewAlert(message: String) {
// アラートを作成
var alertController = UIAlertController(title: "結果", message: message, preferredStyle: .Alert)
// 再挑戦ボタンを追加
alertController.addAction(
UIAlertAction(title: "再挑戦", style: .Default, handler: {action in self.viewDidLoad()})
)
// アラートを表示する
presentViewController(alertController, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment