回答編集履歴
1
追記
test
CHANGED
@@ -83,3 +83,451 @@
|
|
83
83
|
|
84
84
|
|
85
85
|
何をやってるのかを理解したあとにデリゲートパタンにリファクタリングするのが良いと思います。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
## 追記:サンプルコード
|
90
|
+
|
91
|
+
ViewController.swift
|
92
|
+
|
93
|
+
```Swift
|
94
|
+
|
95
|
+
import UIKit
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
/*
|
100
|
+
|
101
|
+
PickerViewは乗ってないからUIPickerViewDelegate,UIPickerViewDataSourceはいらない
|
102
|
+
|
103
|
+
*/
|
104
|
+
|
105
|
+
class ViewController: UIViewController /*,UIPickerViewDelegate,UIPickerViewDataSource*/ {
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
var data:[String] = []
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
@IBOutlet weak var mytableView: UITableView!
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
// var pickerView = UIPickerView()
|
118
|
+
|
119
|
+
// let number = ["回","lep"]
|
120
|
+
|
121
|
+
// let datalist2 = ["kg","lbs"]
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
@IBAction func customalertbtr(_ sender: Any) {
|
126
|
+
|
127
|
+
let customAlert = CustomAlert(frame: CGRect(x: 0.0, y: 0.0, width: 350 , height: 275))
|
128
|
+
|
129
|
+
customAlert.center = self.view.center
|
130
|
+
|
131
|
+
customAlert.backgroundColor = .orange
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
// ViewControllerの参照をセット
|
136
|
+
|
137
|
+
customAlert.delegate = self
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
//print(customAlert) //これ追加
|
142
|
+
|
143
|
+
self.view.addSubview(customAlert)
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
override func viewDidLoad() {
|
150
|
+
|
151
|
+
super.viewDidLoad()
|
152
|
+
|
153
|
+
// Do any additional setup after loading the view, typically from a nib.
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
mytableView.dataSource = self
|
158
|
+
|
159
|
+
mytableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
override func didReceiveMemoryWarning() {
|
168
|
+
|
169
|
+
super.didReceiveMemoryWarning()
|
170
|
+
|
171
|
+
// Dispose of any resources that can be recreated.
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
// データ・ソース
|
182
|
+
|
183
|
+
extension ViewController: UITableViewDataSource {
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
// データ数
|
188
|
+
|
189
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
190
|
+
|
191
|
+
return data.count
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
// セクション数
|
198
|
+
|
199
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
200
|
+
|
201
|
+
return 1
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
// セルの高さ
|
208
|
+
|
209
|
+
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
210
|
+
|
211
|
+
return 40
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
// セル生成
|
218
|
+
|
219
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
|
224
|
+
|
225
|
+
cell.textLabel?.text = data[indexPath.row]
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
return cell
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
extension ViewController: CustomAlertDelegate {
|
238
|
+
|
239
|
+
func appendData(str:String) {
|
240
|
+
|
241
|
+
// CustomAlertで作成した文字列をデータに追加
|
242
|
+
|
243
|
+
data.append(str)
|
244
|
+
|
245
|
+
// tableViewをリロード
|
246
|
+
|
247
|
+
mytableView.reloadData()
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
```
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
CustomAlert.swift
|
260
|
+
|
261
|
+
```Swift
|
262
|
+
|
263
|
+
import UIKit
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// プロトコル
|
268
|
+
|
269
|
+
protocol CustomAlertDelegate {
|
270
|
+
|
271
|
+
func appendData(str:String)
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
class CustomAlert: UIView ,UIPickerViewDataSource,UIPickerViewDelegate {
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
//lepsに関して
|
282
|
+
|
283
|
+
let item = ["-5","-4","-3","-2","-1","0","1","2","3","4","5",]
|
284
|
+
|
285
|
+
var lepsSelectedIndex = 0
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
let kgLbsArr = ["円","ドル"]
|
290
|
+
|
291
|
+
var kgLbsSelectedIndex = 0
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
let difficulityArr = ["安い","普通","高い"]
|
296
|
+
|
297
|
+
var difficulitySelectedIndex = 0
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
var delegate:CustomAlertDelegate? // 他クラス(今回はViewController)の参照を格納するための変数
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
@IBOutlet var myview: UIView!
|
306
|
+
|
307
|
+
@IBOutlet weak var titlelbl: UILabel!
|
308
|
+
|
309
|
+
@IBOutlet weak var exlbl: UILabel!
|
310
|
+
|
311
|
+
@IBOutlet weak var lepstxt: UITextField!
|
312
|
+
|
313
|
+
@IBOutlet weak var kgtxt: UITextField!
|
314
|
+
|
315
|
+
@IBOutlet weak var lepslbl: UILabel!
|
316
|
+
|
317
|
+
@IBOutlet weak var lepspicker: UIPickerView!
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
@IBAction func kglbs(_ sender: Any)
|
322
|
+
|
323
|
+
{
|
324
|
+
|
325
|
+
switch (sender as AnyObject).selectedSegmentIndex {
|
326
|
+
|
327
|
+
case 0:
|
328
|
+
|
329
|
+
//mytableView.text = "kg"
|
330
|
+
|
331
|
+
print("kg")
|
332
|
+
|
333
|
+
case 1:
|
334
|
+
|
335
|
+
//mytableView.text = "lbs"
|
336
|
+
|
337
|
+
print("lbs")
|
338
|
+
|
339
|
+
default: break
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
kgLbsSelectedIndex = (sender as! UISegmentedControl).selectedSegmentIndex
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
@IBAction func difficulty(_ sender: Any)
|
352
|
+
|
353
|
+
{
|
354
|
+
|
355
|
+
switch (sender as AnyObject).selectedSegmentIndex {
|
356
|
+
|
357
|
+
case 0:
|
358
|
+
|
359
|
+
kgtxt.textColor = UIColor.blue
|
360
|
+
|
361
|
+
case 1:
|
362
|
+
|
363
|
+
kgtxt.textColor = UIColor.red
|
364
|
+
|
365
|
+
case 2: //3:
|
366
|
+
|
367
|
+
kgtxt.textColor = UIColor.darkGray
|
368
|
+
|
369
|
+
default: break
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
difficulitySelectedIndex = (sender as! UISegmentedControl).selectedSegmentIndex
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
//コードから
|
382
|
+
|
383
|
+
override init(frame: CGRect) {
|
384
|
+
|
385
|
+
super.init(frame: frame)
|
386
|
+
|
387
|
+
self.commonInit()
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
//ストボーから
|
394
|
+
|
395
|
+
required init?(coder aDecoder: NSCoder) {
|
396
|
+
|
397
|
+
super.init(coder: aDecoder)
|
398
|
+
|
399
|
+
self.commonInit()
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
fileprivate func commonInit() {
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
guard let view = UINib(nibName: "CustomAlert", bundle: nil).instantiate(withOwner: self, options: nil).first as? UIView else {
|
410
|
+
|
411
|
+
return
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
//textfieldに関して
|
418
|
+
|
419
|
+
lepstxt.placeholder = "個数を入力してください。"
|
420
|
+
|
421
|
+
kgtxt.placeholder = "値段を入力してください。"
|
422
|
+
|
423
|
+
lepstxt.font = UIFont(name: "Hiragino Sans", size: 11)
|
424
|
+
|
425
|
+
kgtxt.font = UIFont(name: "Hiragino Sans", size: 11)
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
//デリゲート設定
|
430
|
+
|
431
|
+
lepspicker.delegate = self
|
432
|
+
|
433
|
+
lepspicker.dataSource = self
|
434
|
+
|
435
|
+
//lepspicker.selectedRow(inComponent: 6)
|
436
|
+
|
437
|
+
//lepspicker.backgroundColor = .gray
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
view.frame = self.bounds
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
self.addSubview(view)
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
@IBAction func cancel(_ sender: Any) {
|
454
|
+
|
455
|
+
self.removeFromSuperview()
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
@IBAction func ok(_ sender: Any) {
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
let lepsVal = lepstxt.text
|
464
|
+
|
465
|
+
let kgtVal = kgtxt.text
|
466
|
+
|
467
|
+
let lepsLbl = lepslbl.text
|
468
|
+
|
469
|
+
let num = item[lepsSelectedIndex]
|
470
|
+
|
471
|
+
//mytbleView.text = String(item,array)
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
let cellString = lepsVal! + "が" + num + "(" + lepsLbl! + ")" + " " + kgtVal! + "[" + kgLbsArr[kgLbsSelectedIndex] + "]" + " " + difficulityArr[difficulitySelectedIndex]
|
476
|
+
|
477
|
+
// ViewControllerのメソッドを実行
|
478
|
+
|
479
|
+
delegate?.appendData(str: cellString)
|
480
|
+
|
481
|
+
// アラート閉じる
|
482
|
+
|
483
|
+
self.removeFromSuperview()
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
// PickerViewの列数
|
490
|
+
|
491
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
492
|
+
|
493
|
+
return 1
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
// PickerViewの行数
|
500
|
+
|
501
|
+
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
502
|
+
|
503
|
+
return item.count
|
504
|
+
|
505
|
+
}
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
// PickerViewの項目
|
510
|
+
|
511
|
+
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
512
|
+
|
513
|
+
return item[row]
|
514
|
+
|
515
|
+
}
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
// PickerViewの項目選択時
|
520
|
+
|
521
|
+
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
522
|
+
|
523
|
+
lepsSelectedIndex = row
|
524
|
+
|
525
|
+
}
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
}
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
```
|