Skip to content

Instantly share code, notes, and snippets.

@masakid
Last active August 29, 2015 14:16
Show Gist options
  • Save masakid/5c98711fb530367e115b to your computer and use it in GitHub Desktop.
Save masakid/5c98711fb530367e115b to your computer and use it in GitHub Desktop.
lesson2
//
// ViewController.swift
// Problem_part2
//
// Created by 久保田 将規 on 2015/03/08.
// 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 segmentedControl: UISegmentedControl!
@IBOutlet weak var resultLabel: UILabel!
let plus : String = "+"
let minus : String = "-"
let multiply : String = "×"
let divide : String = "÷"
override func viewDidLoad() {
super.viewDidLoad()
self.resultLabel.text = ""
// 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 pressResultButton(sender: AnyObject) {
let target1 = (self.textField1.text as NSString).doubleValue ?? 0
let target2 = (self.textField2.text as NSString).doubleValue ?? 0
let num = self.segmentedControl.selectedSegmentIndex
var total = ""
if let title = self.segmentedControl.titleForSegmentAtIndex(num) {
switch title {
case plus:
total = "\(target1 + target2)"
case minus:
total = "\(target1 - target2)"
case multiply:
total = "\(target1 * target2)"
case divide:
if target2 == 0.0 {
total = "割る数には0以外を入力して下さい"
} else {
total = "\(target1 / target2)"
}
default:
total = ""
}
self.resultLabel.text = total
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment