Skip to content

Instantly share code, notes, and snippets.

@masakid
Last active August 29, 2015 14:17
Show Gist options
  • Save masakid/6d71e3d009f6862b876e to your computer and use it in GitHub Desktop.
Save masakid/6d71e3d009f6862b876e to your computer and use it in GitHub Desktop.
lesson6
//
// ViewController.swift
// Problem_part6
//
// Created by 久保田 将規 on 2015/03/14.
// Copyright (c) 2015年 masaki.k. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var problemLabel: UILabel!
@IBOutlet weak var answerSlider: UISlider!
var problemValue:Int!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
initialize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//判定ボタン押下処理
@IBAction func pressJudgeAnswer(sender: AnyObject) {
let answerValue = Int(self.answerSlider.value)
if answerValue == problemValue {
let output = "あたり!\n あなたの値: \(answerValue)"
showAlertWithMessage(output)
} else {
let output = "はずれ!\n あなたの値: \(answerValue)"
showAlertWithMessage(output)
}
}
//アラート処理
func showAlertWithMessage(output:String){
let alert = UIAlertController(title: "結果", message: output, preferredStyle: .Alert)
//再挑戦ボタン作成(handlerでこのように指定するとボタン押下時の処理となる)
let retryButton = UIAlertAction(title: "再挑戦", style: .Default, handler: {action in self.initialize()})
alert.addAction(retryButton)
//アラートの表示
presentViewController(alert, animated: true, completion: nil)
}
//初期化メソッド
func initialize(){
problemValue = Int(arc4random_uniform(100)) + 1
self.problemLabel.text = "\(problemValue)"
self.answerSlider.value = 50
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment