Last active
June 14, 2023 07:57
-
-
Save kenji272/427bf10543d2a9dbc746 to your computer and use it in GitHub Desktop.
【課題】Part1 5つの数字を足し算するアプリを作る
This file contains 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
import UIKit | |
class ViewController: UIViewController { | |
// textFieldCollection | |
// 足し算アプリのため、テキストフィールドの順序は保証しなくて良い | |
@IBOutlet var numberTextFieldCollection: [UITextField]! | |
// 結果を表示するラベル | |
@IBOutlet weak var resultLabel: UILabel! | |
// ジェスチャー | |
@IBOutlet var tapGesture: UITapGestureRecognizer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// ジェスチャー追加 | |
self.view.addGestureRecognizer(self.tapGesture) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// 足し算を行う | |
@IBAction func addNumber(sender: AnyObject) { | |
var sum: Int = 0 | |
for val in self.numberTextFieldCollection { | |
if val.text.isEmpty { | |
continue | |
} | |
// ナンバーパッドで入力のため不正入力チェックは行わない | |
var num = (val.text as NSString).integerValue | |
sum = sum + num | |
} | |
self.resultLabel.text = "\(sum)" | |
} | |
// キーボード外をタップしてナンバーパッドを閉じる | |
// TODO View->identity inspector->Accessibility->Traits->UserInteraction Enabled | |
// のチェックを外さないと実行されない | |
@IBAction func closeNumberPad(sender: UITapGestureRecognizer) { | |
self.view.endEditing(true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment