-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewController.swift
More file actions
44 lines (33 loc) · 1.22 KB
/
ViewController.swift
File metadata and controls
44 lines (33 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// ViewController.swift
// mobile-programming-ios
//
// Created by Yubin on 5/3/25.
//
import UIKit
class ViewController: UIViewController {
// Hold the control button and drag the widget here
@IBOutlet var resultLabel: UILabel!
@IBOutlet var firstNumField: UITextField!
@IBOutlet var secondNumField: UITextField!
@IBAction func addButton(_ sender: Any) {
var result: Double = 0;
let firstValue = Double(firstNumField.text!) ?? 0.0
let secondValue = Double(secondNumField.text!) ?? 0.0
result = firstValue + secondValue
let formattedResult = String(format: "%.2f", arguments: [result])
resultLabel.text = "\(formattedResult)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Create a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
// Make it not interfere with other controls
tapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(tapGesture)
}
@objc func dismissKeyboard() {
// Resign first responder status from any first responder (typically the active keyboard)
view.endEditing(true)
}
}