Advertisement
Guest User

Untitled

a guest
May 27th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5. // textFieldCollection
  6. // 足し算アプリのため、テキストフィールドの順序は保証しなくて良い
  7. @IBOutlet var numberTextFieldCollection: [UITextField]!
  8.  
  9. // 結果を表示するラベル
  10. @IBOutlet weak var resultLabel: UILabel!
  11.  
  12. // ジェスチャー
  13. @IBOutlet var tapGesture: UITapGestureRecognizer!
  14.  
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17.  
  18. // ジェスチャー追加
  19. self.view.addGestureRecognizer(self.tapGesture)
  20.  
  21. }
  22.  
  23. override func didReceiveMemoryWarning() {
  24. super.didReceiveMemoryWarning()
  25. // Dispose of any resources that can be recreated.
  26. }
  27.  
  28. // 足し算を行う
  29. @IBAction func addNumber(sender: AnyObject) {
  30.  
  31. var sum: Int = 0
  32. for val in self.numberTextFieldCollection {
  33. if val.text.isEmpty {
  34. continue
  35. }
  36.  
  37. // ナンバーパッドで入力のため不正入力チェックは行わない
  38. var num = (val.text as NSString).integerValue
  39. sum = sum + num
  40. }
  41. self.resultLabel.text = "\(sum)"
  42. }
  43.  
  44. // キーボード外をタップしてナンバーパッドを閉じる
  45. // TODO View->identity inspector->Accessibility->Traits->UserInteraction Enabled
  46. // のチェックを外さないと実行されない
  47. @IBAction func closeNumberPad(sender: UITapGestureRecognizer) {
  48. self.view.endEditing(true)
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement