質問編集履歴

7

変更

2018/06/18 13:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes

6

変更

2018/06/18 13:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes

5

変更

2018/06/18 07:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes

4

変更

2018/06/18 03:50

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes

3

変更

2018/06/18 02:15

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,86 +1,256 @@
1
- ストーリーボードを使わずにコードのみでアプリを作っています。
2
-
3
-
4
-
5
1
  FirstViewController(UIViewController)から
6
2
 
7
3
  SecondViewController(UICollectionViewCell)へ値を渡す方法をコードのみでの実現したいと思っています。
8
4
 
9
5
 
10
6
 
11
- FirstViewController内のUITextFieldにテキストを入力しUIButtonをタップしても、
12
-
13
- 下記コードの //ここで値したい の部分で、
7
+ 一連の動作をする4ファイルのコードを記載まし
14
-
15
- SecondViewController内のUILabelへ 値を渡せていない為テキストの反映がありません。
8
+
16
-
17
-
18
-
19
- 下記コードの HogeStrngは、SecondViewController内にある var HogeStrng:String! で、値を中継する様に間に挟んでいます。
20
-
21
-
22
-
23
- UITextField.text = "", UILabel.text = ""と、初期化しています。
24
-
25
-
26
-
27
- 各オブジェクトはシミュレータ上で反映、操作自体は確認できています。
9
+ 確認いただければ幸いす よろしくお願ます。
28
-
29
-
30
-
31
-
32
10
 
33
11
  ```ここに言語を入力
34
12
 
35
- FirstViewController内の一部
36
-
37
-
38
-
39
- ...省略
40
-
41
-
42
-
43
- //UIButtonをタップし最後に発火する関数
44
-
45
-
46
-
47
- @objc func save(_ sender: UITapGestureRecognizer) {
48
-
49
-
50
-
51
-
52
-
53
- //ここで値を渡したい
54
-
55
- if userName.text != nil {
56
-
57
- SecondViewController().HogeStrng = UITextField.text!
58
-
59
- SecondViewController().UILabel.text = SecondViewController(). HogeStrng
60
-
61
-
62
-
63
- }
64
-
65
-
13
+
14
+
15
+ import UIKit
16
+
17
+
18
+
19
+ class PresentationController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
20
+
21
+
22
+
23
+ private var Button: UIBarButtonItem!
24
+
25
+ private let cellId = "cellId"
26
+
27
+
28
+
29
+ override func viewDidLoad() {
30
+
31
+ super.viewDidLoad()
32
+
33
+ set()
34
+
35
+ }
36
+
37
+
38
+
39
+ func set() {
40
+
41
+
42
+
43
+ collectionView?.delegate = self
44
+
45
+ collectionView?.dataSource = self
46
+
47
+
48
+
49
+ collectionView?.register(SecondViewController.self, forCellWithReuseIdentifier: "cellId")
50
+
51
+
52
+
53
+ }
54
+
55
+
56
+
57
+ override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
58
+
59
+ return 1
60
+
61
+ }
62
+
63
+
64
+
65
+ override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
66
+
67
+
68
+
69
+ switch indexPath.item {
70
+
71
+ default:
72
+
73
+ return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! SecondViewController
74
+
75
+
76
+
77
+ }
78
+
79
+ }
80
+
81
+
82
+
83
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
84
+
85
+
86
+
87
+ switch indexPath.item {
88
+
89
+ default:
90
+
91
+ return CGSize(width: view.frame.width, height: 300)
92
+
93
+ }
94
+
95
+ }
96
+
97
+
98
+
99
+ //ここへ
100
+
101
+ @objc func openButton(_ sender: UIButton) {
102
+
103
+ print("変更するモーダルビュー 開いたよ")
104
+
105
+ //モーダルの動き .custom
106
+
107
+ let modalViewController = FirstViewController()
108
+
109
+ modalViewController.modalPresentationStyle = .custom
110
+
111
+ modalViewController.transitioningDelegate = self
112
+
113
+ present(modalViewController, animated: true, completion: nil)
114
+
115
+ }
116
+
117
+
118
+
119
+ //
120
+
121
+ override func viewWillAppear(_ animated: Bool) {
122
+
123
+ collectionView?.reloadData()
124
+
125
+ }
126
+
127
+ }
128
+
129
+ ```
130
+
131
+
132
+
133
+ ```ここに言語を入力
134
+
135
+
136
+
137
+ import UIKit
138
+
139
+
140
+
141
+ class CustomPresentationController: UIPresentationController {
142
+
143
+ // 呼び出し元のView Controller の上に重ねるビュー(ブラック)
144
+
145
+ var overlayView = UIView()
146
+
147
+
148
+
149
+ override func presentationTransitionWillBegin() {
150
+
151
+ guard let containerView = containerView else {
152
+
153
+ return
154
+
155
+ }
66
156
 
67
157
 
68
158
 
159
+ overlayView.frame = containerView.bounds
160
+
161
+ overlayView.backgroundColor = .black
162
+
69
- saveButton.endEditing(true)
163
+ overlayView.alpha = 0.0
164
+
70
-
165
+ containerView.insertSubview(overlayView, at: 0)
166
+
167
+
168
+
71
-
169
+ presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
170
+
72
-
171
+ self?.overlayView.alpha = 0.7
172
+
173
+ }, completion:nil)
174
+
175
+ }
176
+
177
+
178
+
179
+ override func dismissalTransitionWillBegin() {
180
+
181
+ presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
182
+
183
+ self?.overlayView.alpha = 0.0
184
+
185
+ }, completion:nil)
186
+
187
+ }
188
+
189
+
190
+
73
- self.dismiss(animated: true, completion: nil)
191
+ override func dismissalTransitionDidEnd(_ completed: Bool) {
192
+
74
-
193
+ if completed {
194
+
75
- print("タップ キーボードとモーダルビュー 閉じたよ")
195
+ overlayView.removeFromSuperview()
76
-
77
-
78
196
 
79
197
  }
80
198
 
81
-
82
-
83
- ...省略
199
+ }
200
+
201
+
202
+
203
+ let margin = (x: CGFloat(30), y: CGFloat(220.0))
204
+
205
+
206
+
207
+ override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
208
+
209
+ return CGSize(width: parentSize.width - margin.x, height: parentSize.height - margin.y)
210
+
211
+ }
212
+
213
+
214
+
215
+ override var frameOfPresentedViewInContainerView: CGRect {
216
+
217
+ var presentedViewFrame = CGRect()
218
+
219
+ let containerBounds = containerView!.bounds
220
+
221
+ let childContentSize = size(forChildContentContainer: presentedViewController, withParentContainerSize: containerBounds.size)
222
+
223
+ presentedViewFrame.size = childContentSize
224
+
225
+ presentedViewFrame.origin.x = margin.x / 2.0
226
+
227
+ presentedViewFrame.origin.y = margin.y / 2.0
228
+
229
+
230
+
231
+ return presentedViewFrame
232
+
233
+ }
234
+
235
+
236
+
237
+ override func containerViewWillLayoutSubviews() {
238
+
239
+ overlayView.frame = containerView!.bounds //bounds 境界内
240
+
241
+ presentedView?.frame = frameOfPresentedViewInContainerView
242
+
243
+ presentedView?.layer.cornerRadius = 10
244
+
245
+ presentedView?.clipsToBounds = true
246
+
247
+ }
248
+
249
+ override func containerViewDidLayoutSubviews() {
250
+
251
+ }
252
+
253
+ }
84
254
 
85
255
 
86
256
 
@@ -88,6 +258,386 @@
88
258
 
89
259
 
90
260
 
261
+ ```ここに言語を入力
262
+
263
+
264
+
265
+ import UIKit
266
+
267
+
268
+
269
+ class FirstViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
270
+
271
+
272
+
273
+ override func viewDidLoad() {
274
+
275
+ super.viewDidLoad()
276
+
277
+ view.backgroundColor = .white
278
+
279
+ set()
280
+
281
+ }
282
+
283
+
284
+
285
+ let userName: UITextField = {
286
+
287
+ let un = UITextField()
288
+
289
+ un.backgroundColor = .gray
290
+
291
+ un.text = ""
292
+
91
- 値を渡す際のリレーがうまく行っていないのでしょうか?いくつも思い当たる事を2週間以上かけ試みていますが解決できません。
293
+ un.frame = CGRect(x: 100, y: 120, width: 200, height: 40)
294
+
92
-
295
+ un.returnKeyType = .done
296
+
297
+ return un
298
+
299
+ }()
300
+
301
+
302
+
303
+ let saveButton: UIButton = {
304
+
305
+ let c = UIButton()
306
+
307
+ c.frame = CGRect(x: 200, y: 0, width: 100, height: 100)
308
+
309
+ c.setTitle("保存", for: .normal)
310
+
93
- どうぞ知恵をお貸しください。よろしくお願いします。
311
+ c.backgroundColor = .gray
312
+
313
+ return c
314
+
315
+ }()
316
+
317
+
318
+
319
+ let cancelbutton: UIButton = {
320
+
321
+ let b = UIButton()
322
+
323
+ b.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
324
+
325
+ b.setTitle("キャンセル", for: .normal)
326
+
327
+ b.backgroundColor = .green
328
+
329
+ return b
330
+
331
+ }()
332
+
333
+
334
+
335
+ func set() {
336
+
337
+ //初期化時 カラにする
338
+
339
+ userName.text = ""
340
+
341
+ view.addSubview(userName)
342
+
343
+ userName.delegate = self
344
+
345
+
346
+
347
+ view.addSubview(saveButton)
348
+
349
+ //初期化時は タップ無効化
350
+
351
+ saveButton.isEnabled = false
352
+
353
+ //ターゲット このボタンをダイレクトにタップした時の判定は⬇️の パターン2にあるよ
354
+
355
+ saveButton.addTarget(self, action: #selector(save2(_:)), for: .touchUpInside)
356
+
357
+
358
+
359
+ view.addSubview(cancelbutton)
360
+
361
+ cancelbutton.addTarget(self, action: #selector(close(_:)), for: .touchUpInside)
362
+
363
+ }
364
+
365
+
366
+
367
+
368
+
369
+ //パターン????
370
+
371
+ //"return"をタップ キーボード閉じる
372
+
373
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
374
+
375
+
376
+
377
+ //"return"でtextFieldが閉じる(userName, place, siteなど)
378
+
379
+ textField.resignFirstResponder()
380
+
381
+
382
+
383
+ //TextFieldに入力された値に反応して、その値を習得。
384
+
385
+ NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeNotifyTextField(sender:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
386
+
387
+
388
+
389
+ return true
390
+
391
+ }
392
+
393
+
394
+
395
+ //senderの中に 変更があったUITextFieldが入ってくる感じ
396
+
397
+ @objc public func changeNotifyTextField (sender: NSNotification) {
398
+
399
+
400
+
401
+ guard let textView = sender.object as? UITextField else { return }
402
+
403
+
404
+
405
+ //文字が1以上で有効に
406
+
407
+ saveButton.isEnabled = (userName.text?.count)! > 0
408
+
409
+
410
+
411
+ //タップ処理を登録
412
+
413
+ saveButton.addTarget(self, action: #selector(save3(_:)), for: .touchUpInside)
414
+
415
+ }
416
+
417
+
418
+
419
+ //????
420
+
421
+
422
+
423
+ //パターン????
424
+
425
+ //saveButtonをダイレクトにタップ
426
+
427
+ @objc func save2(_ sender: UITapGestureRecognizer) {
428
+
429
+
430
+
431
+ NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeNotifyTextField2(sender:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
432
+
433
+ }
434
+
435
+
436
+
437
+
438
+
439
+ //senderの中に 変更があったUITextFieldが入ってくる感じ
440
+
441
+ @objc public func changeNotifyTextField2 (sender: NSNotification) {
442
+
443
+
444
+
445
+ guard let textView = sender.object as? UITextField else {
446
+
447
+ return
448
+
449
+ }
450
+
451
+ //文字が1以上で有効に
452
+
453
+ saveButton.isEnabled = (userName.text?.count)! > 0
454
+
455
+ //タップ処理を登録
456
+
457
+ saveButton.addTarget(self, action: #selector(save3(_:)), for: .touchUpInside)
458
+
459
+ }
460
+
461
+
462
+
463
+
464
+
465
+ @objc func save3(_ sender: UITapGestureRecognizer) {
466
+
467
+
468
+
469
+ //値を渡したい
470
+
471
+ let controller = presentingViewController as! SecondViewController
472
+
473
+
474
+
475
+ controller.textFromModal = userName.text!
476
+
477
+
478
+
479
+ self.dismiss(animated: true, completion: nil)
480
+
481
+
482
+
483
+ print("タップ キーボードとモーダルビュー 閉じたよ")
484
+
485
+
486
+
487
+ saveButton.endEditing(true)
488
+
489
+
490
+
491
+ }
492
+
493
+ //????
494
+
495
+ @objc func close(_ sender: UITapGestureRecognizer) {
496
+
497
+ print("保存キャンセル モーダルビュー 閉じたよ")
498
+
499
+ self.dismiss(animated: true, completion: nil)
500
+
501
+ }
502
+
503
+
504
+
505
+ }
506
+
507
+
508
+
509
+
510
+
511
+ ```
512
+
513
+
514
+
515
+ ```ここに言語を入力
516
+
517
+
518
+
519
+ import UIKit
520
+
521
+
522
+
523
+ class SecondViewController: UICollectionViewCell {
524
+
525
+
526
+
527
+ //モーダルビューから受け取るテキスト
528
+
529
+ var textFromModal = "" {
530
+
531
+ didSet {
532
+
533
+ // 値が更新されたら、ラベルの内容も更新する
534
+
535
+ updateLabel(text: textFromModal)
536
+
537
+ }
538
+
539
+ }
540
+
541
+
542
+
543
+ //ラベルの内容を更新する処理
544
+
545
+ func updateLabel(text: String) {
546
+
547
+ nameLabel.text = ""
548
+
549
+ }
550
+
551
+
552
+
553
+ override init(frame: CGRect) {
554
+
555
+ super.init(frame: frame)
556
+
557
+ backgroundColor = .gray
558
+
559
+ set()
560
+
561
+ }
562
+
563
+
564
+
565
+ required init?(coder aDecoder: NSCoder) {
566
+
567
+ fatalError("init(coder:) has not been implemented")
568
+
569
+ }
570
+
571
+
572
+
573
+ let button: UIButton = {
574
+
575
+ let b = UIButton()
576
+
577
+ b.setTitle("モーダル", for: .normal)
578
+
579
+ b.backgroundColor = .blue
580
+
581
+ b.translatesAutoresizingMaskIntoConstraints = false
582
+
583
+ b.addTarget(self, action: #selector(PresentationController.openButton(_:)), for: .touchUpInside)
584
+
585
+ b.frame = CGRect(x: 20, y: 50, width: 100, height: 100)
586
+
587
+ return b
588
+
589
+ }()
590
+
591
+
592
+
593
+
594
+
595
+ let nameLabel: UILabel = {
596
+
597
+ let l = UILabel()
598
+
599
+ l.backgroundColor = .white
600
+
601
+ l.translatesAutoresizingMaskIntoConstraints = false
602
+
603
+ l.frame = CGRect(x: 20, y: 200, width: 200, height: 50)
604
+
605
+
606
+
607
+ return l
608
+
609
+ }()
610
+
611
+
612
+
613
+ func set() {
614
+
615
+ addSubview(button)
616
+
617
+ addSubview(nameLabel)
618
+
619
+ }
620
+
621
+ }
622
+
623
+
624
+
625
+ //UIPresentationControllerで返す クラス名(CustomPresentationController)
626
+
627
+ extension PresentationController: UIViewControllerTransitioningDelegate {
628
+
629
+ func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
630
+
631
+ return CustomPresentationController(presentedViewController: presented, presenting: presenting)
632
+
633
+ }
634
+
635
+
636
+
637
+ }
638
+
639
+
640
+
641
+
642
+
643
+ ```

2

変更

2018/06/17 16:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  SecondViewController().HogeStrng = UITextField.text!
58
58
 
59
- UCCell().UILabel.text = SecondViewController(). HogeStrng
59
+ SecondViewController().UILabel.text = SecondViewController(). HogeStrng
60
60
 
61
61
 
62
62
 

1

変更

2018/06/15 15:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -54,9 +54,9 @@
54
54
 
55
55
  if userName.text != nil {
56
56
 
57
- UCCell().HogeStrng = UITextField.text!
57
+ SecondViewController().HogeStrng = UITextField.text!
58
58
 
59
- UCCell().UILabel.text = UCCell(). HogeStrng
59
+ UCCell().UILabel.text = SecondViewController(). HogeStrng
60
60
 
61
61
 
62
62