質問編集履歴
2
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,4 +15,120 @@
|
|
15
15
|
ソースコードはgitを見て頂けると幸いです。
|
16
16
|
[git: CustomAlert](https://github.com/haruka22/alert)
|
17
17
|
|
18
|
-

|
18
|
+

|
19
|
+
|
20
|
+
### 改良した点
|
21
|
+
```swift
|
22
|
+
import UIKit
|
23
|
+
|
24
|
+
|
25
|
+
class weightView: UIView {
|
26
|
+
|
27
|
+
@IBOutlet weak var weightlbl: UILabel!
|
28
|
+
@IBOutlet weak var bfplbl: UILabel!
|
29
|
+
@IBOutlet weak var choice: UISegmentedControl!
|
30
|
+
|
31
|
+
var delegate:CustomAlertDelegate?
|
32
|
+
var dataSource = DataSource()
|
33
|
+
|
34
|
+
|
35
|
+
override init(frame: CGRect){
|
36
|
+
super.init(frame: frame)
|
37
|
+
loadNib()
|
38
|
+
}
|
39
|
+
|
40
|
+
required init(coder aDecoder: NSCoder) {
|
41
|
+
super.init(coder: aDecoder)!
|
42
|
+
loadNib()
|
43
|
+
}
|
44
|
+
|
45
|
+
func loadNib(){
|
46
|
+
if let view = Bundle.main.loadNibNamed("weightView", owner: self, options: nil)?.first as? UIView {
|
47
|
+
view.frame = self.bounds
|
48
|
+
self.addSubview(view)
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
@IBAction func selectedSegment(_ sender: UISegmentedControl) {
|
53
|
+
guard let selectedType = DataSource.InputType(rawValue: sender.selectedSegmentIndex) else { return }
|
54
|
+
dataSource.inputType = selectedType
|
55
|
+
}
|
56
|
+
|
57
|
+
@IBAction func tapNumber(_ sender: UIButton) {
|
58
|
+
let text = sender.tag == 10 ? "." : String(sender.tag)
|
59
|
+
dataSource.upDate(value: text)
|
60
|
+
labelUpdate()
|
61
|
+
}
|
62
|
+
|
63
|
+
func labelUpdate() {
|
64
|
+
weightlbl.text = dataSource.wValue + "kg"
|
65
|
+
bfplbl.text = dataSource.bValue + "%"
|
66
|
+
print(dataSource.wValue)
|
67
|
+
print(dataSource.bValue)
|
68
|
+
}
|
69
|
+
|
70
|
+
@IBAction func clear(_ sender: UIButton) {
|
71
|
+
dataSource.clear()
|
72
|
+
labelUpdate()
|
73
|
+
}
|
74
|
+
|
75
|
+
@IBAction func OKbtr(_ sender: Any) {
|
76
|
+
let cellString = "体重" + weightlbl.text! + "体脂肪率" + bfplbl.text!
|
77
|
+
delegate?.appendData(str: cellString, color: bfplbl.textColor)
|
78
|
+
self.removeFromSuperview()
|
79
|
+
}
|
80
|
+
|
81
|
+
@IBAction func CANCELbtr(_ sender: Any) {
|
82
|
+
self.removeFromSuperview()
|
83
|
+
}
|
84
|
+
|
85
|
+
class DataSource {
|
86
|
+
|
87
|
+
enum InputType: Int {
|
88
|
+
case p, f
|
89
|
+
}
|
90
|
+
|
91
|
+
var inputType: InputType = .p
|
92
|
+
var wValue = ""
|
93
|
+
var bValue = ""
|
94
|
+
|
95
|
+
func upDate(value: String) {
|
96
|
+
switch inputType {
|
97
|
+
case .p:
|
98
|
+
guard (wValue + value).numberOfOccurrences(of: ".") < 2 else { return }
|
99
|
+
wValue += value
|
100
|
+
case .f:
|
101
|
+
guard (bValue + value).numberOfOccurrences(of: ".") < 2 else { return }
|
102
|
+
bValue += value
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
func clear() {
|
107
|
+
switch inputType {
|
108
|
+
case .p:
|
109
|
+
wValue = ""
|
110
|
+
case .f: bValue = ""
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
```
|
117
|
+
|
118
|
+
```swift
|
119
|
+
import Foundation
|
120
|
+
|
121
|
+
extension String {
|
122
|
+
func numberOfOccurrences(of word: String) -> Int {
|
123
|
+
var count = 0
|
124
|
+
var nextRange = self.startIndex..<self.endIndex
|
125
|
+
while let range = self.range(of: word, options: .caseInsensitive, range: nextRange) {
|
126
|
+
count += 1
|
127
|
+
nextRange = range.upperBound..<self.endIndex
|
128
|
+
}
|
129
|
+
return count
|
130
|
+
}
|
131
|
+
}
|
132
|
+
```
|
133
|
+
|
134
|
+
入力されるlabelは2つになっています。
|
1
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,4 +13,6 @@
|
|
13
13
|
|
14
14
|
|
15
15
|
ソースコードはgitを見て頂けると幸いです。
|
16
|
-
[git: CustomAlert](https://github.com/haruka22/alert)
|
16
|
+
[git: CustomAlert](https://github.com/haruka22/alert)
|
17
|
+
|
18
|
+

|