質問編集履歴

2

コードの追加

2018/07/24 02:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -33,3 +33,235 @@
33
33
 
34
34
 
35
35
  ![イメージ説明](0326b6a901ab673dbc7395a21f9e27e7.png)
36
+
37
+
38
+
39
+ ### 改良した点
40
+
41
+ ```swift
42
+
43
+ import UIKit
44
+
45
+
46
+
47
+
48
+
49
+ class weightView: UIView {
50
+
51
+
52
+
53
+ @IBOutlet weak var weightlbl: UILabel!
54
+
55
+ @IBOutlet weak var bfplbl: UILabel!
56
+
57
+ @IBOutlet weak var choice: UISegmentedControl!
58
+
59
+
60
+
61
+ var delegate:CustomAlertDelegate?
62
+
63
+ var dataSource = DataSource()
64
+
65
+
66
+
67
+
68
+
69
+ override init(frame: CGRect){
70
+
71
+ super.init(frame: frame)
72
+
73
+ loadNib()
74
+
75
+ }
76
+
77
+
78
+
79
+ required init(coder aDecoder: NSCoder) {
80
+
81
+ super.init(coder: aDecoder)!
82
+
83
+ loadNib()
84
+
85
+ }
86
+
87
+
88
+
89
+ func loadNib(){
90
+
91
+ if let view = Bundle.main.loadNibNamed("weightView", owner: self, options: nil)?.first as? UIView {
92
+
93
+ view.frame = self.bounds
94
+
95
+ self.addSubview(view)
96
+
97
+ }
98
+
99
+ }
100
+
101
+
102
+
103
+ @IBAction func selectedSegment(_ sender: UISegmentedControl) {
104
+
105
+ guard let selectedType = DataSource.InputType(rawValue: sender.selectedSegmentIndex) else { return }
106
+
107
+ dataSource.inputType = selectedType
108
+
109
+ }
110
+
111
+
112
+
113
+ @IBAction func tapNumber(_ sender: UIButton) {
114
+
115
+ let text = sender.tag == 10 ? "." : String(sender.tag)
116
+
117
+ dataSource.upDate(value: text)
118
+
119
+ labelUpdate()
120
+
121
+ }
122
+
123
+
124
+
125
+ func labelUpdate() {
126
+
127
+ weightlbl.text = dataSource.wValue + "kg"
128
+
129
+ bfplbl.text = dataSource.bValue + "%"
130
+
131
+ print(dataSource.wValue)
132
+
133
+ print(dataSource.bValue)
134
+
135
+ }
136
+
137
+
138
+
139
+ @IBAction func clear(_ sender: UIButton) {
140
+
141
+ dataSource.clear()
142
+
143
+ labelUpdate()
144
+
145
+ }
146
+
147
+
148
+
149
+ @IBAction func OKbtr(_ sender: Any) {
150
+
151
+ let cellString = "体重" + weightlbl.text! + "体脂肪率" + bfplbl.text!
152
+
153
+ delegate?.appendData(str: cellString, color: bfplbl.textColor)
154
+
155
+ self.removeFromSuperview()
156
+
157
+ }
158
+
159
+
160
+
161
+ @IBAction func CANCELbtr(_ sender: Any) {
162
+
163
+ self.removeFromSuperview()
164
+
165
+ }
166
+
167
+
168
+
169
+ class DataSource {
170
+
171
+
172
+
173
+ enum InputType: Int {
174
+
175
+ case p, f
176
+
177
+ }
178
+
179
+
180
+
181
+ var inputType: InputType = .p
182
+
183
+ var wValue = ""
184
+
185
+ var bValue = ""
186
+
187
+
188
+
189
+ func upDate(value: String) {
190
+
191
+ switch inputType {
192
+
193
+ case .p:
194
+
195
+ guard (wValue + value).numberOfOccurrences(of: ".") < 2 else { return }
196
+
197
+ wValue += value
198
+
199
+ case .f:
200
+
201
+ guard (bValue + value).numberOfOccurrences(of: ".") < 2 else { return }
202
+
203
+ bValue += value
204
+
205
+ }
206
+
207
+
208
+
209
+ }
210
+
211
+ func clear() {
212
+
213
+ switch inputType {
214
+
215
+ case .p:
216
+
217
+ wValue = ""
218
+
219
+ case .f: bValue = ""
220
+
221
+ }
222
+
223
+ }
224
+
225
+ }
226
+
227
+
228
+
229
+ }
230
+
231
+ ```
232
+
233
+
234
+
235
+ ```swift
236
+
237
+ import Foundation
238
+
239
+
240
+
241
+ extension String {
242
+
243
+ func numberOfOccurrences(of word: String) -> Int {
244
+
245
+ var count = 0
246
+
247
+ var nextRange = self.startIndex..<self.endIndex
248
+
249
+ while let range = self.range(of: word, options: .caseInsensitive, range: nextRange) {
250
+
251
+ count += 1
252
+
253
+ nextRange = range.upperBound..<self.endIndex
254
+
255
+ }
256
+
257
+ return count
258
+
259
+ }
260
+
261
+ }
262
+
263
+ ```
264
+
265
+
266
+
267
+ 入力されるlabelは2つになっています。

1

画像の追加

2018/07/24 02:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,7 @@
29
29
  ソースコードはgitを見て頂けると幸いです。
30
30
 
31
31
  [git: CustomAlert](https://github.com/haruka22/alert)
32
+
33
+
34
+
35
+ ![イメージ説明](0326b6a901ab673dbc7395a21f9e27e7.png)