Last active
August 29, 2015 14:16
-
-
Save masakid/4bfe0ecf04a890f2fda7 to your computer and use it in GitHub Desktop.
lesson5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Problem_part5 | |
// | |
// Created by 久保田 将規 on 2015/03/12. | |
// Copyright (c) 2015年 masaki.k. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var textField1: UITextField! | |
@IBOutlet weak var textField2: UITextField! | |
@IBOutlet weak var resultLabel: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func calcButton(sender: AnyObject) { | |
//入力 | |
let text1 = self.textField1.text | |
let text2 = self.textField2.text | |
//バリデーション | |
if let error = validationValue(text1, text2: text2) { | |
showAlertWithContent(error) | |
return | |
} | |
let doubleVal1 = (text1 as NSString).doubleValue | |
let doubleVal2 = (text2 as NSString).doubleValue | |
let answer = doubleVal1 / doubleVal2 | |
self.resultLabel.text = "\(answer)" | |
} | |
//バリデーションチェック | |
func validationValue(text1:String, text2:String) -> String?{ | |
if text1.isEmpty { | |
return "割られる数を入力してください" | |
} else if text2.isEmpty{ | |
return "割る数を入力してください" | |
} else if text2 == "0" { | |
return "割る数には0を入力しないでください" | |
} else { | |
return nil | |
} | |
} | |
//アラート表示 | |
func showAlertWithContent( content : String){ | |
let alert = UIAlertController(title: "課題5", message: content, preferredStyle: .Alert) | |
let ok = UIAlertAction(title: "ok", style: .Default, handler: nil) | |
alert.addAction(ok) | |
presentViewController(alert, animated: true, completion: nil) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment