質問編集履歴

3

変更

2018/06/19 07:20

投稿

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

2

変更

2018/06/19 07:20

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -138,61 +138,273 @@
138
138
 
139
139
 
140
140
 
141
- class CustomPresentationController: UIPresentationController {
141
+ class FirstViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
142
-
143
- // 呼び出し元のView Controller の上に重ねるビュー(ブラック)
142
+
144
-
145
- var overlayView = UIView()
143
+
146
-
147
-
148
-
144
+
149
- override func presentationTransitionWillBegin() {
145
+ override func viewDidLoad() {
146
+
150
-
147
+ super.viewDidLoad()
148
+
149
+ view.backgroundColor = .white
150
+
151
+ set()
152
+
153
+ }
154
+
155
+
156
+
157
+ let userName: UITextField = {
158
+
159
+ let un = UITextField()
160
+
161
+ un.backgroundColor = .gray
162
+
163
+ un.text = ""
164
+
165
+ un.frame = CGRect(x: 100, y: 120, width: 200, height: 40)
166
+
167
+ un.returnKeyType = .done
168
+
169
+ return un
170
+
171
+ }()
172
+
173
+
174
+
175
+ let saveButton: UIButton = {
176
+
177
+ let c = UIButton()
178
+
179
+ c.frame = CGRect(x: 200, y: 0, width: 100, height: 100)
180
+
181
+ c.setTitle("保存", for: .normal)
182
+
183
+ c.backgroundColor = .gray
184
+
185
+ return c
186
+
187
+ }()
188
+
189
+
190
+
191
+ let cancelbutton: UIButton = {
192
+
193
+ let b = UIButton()
194
+
195
+ b.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
196
+
197
+ b.setTitle("キャンセル", for: .normal)
198
+
199
+ b.backgroundColor = .green
200
+
201
+ return b
202
+
203
+ }()
204
+
205
+
206
+
207
+ func set() {
208
+
209
+ //初期化時 カラにする
210
+
211
+ userName.text = ""
212
+
213
+ view.addSubview(userName)
214
+
215
+ userName.delegate = self
216
+
217
+
218
+
219
+ view.addSubview(saveButton)
220
+
221
+ //初期化時は タップ無効化
222
+
223
+ saveButton.isEnabled = false
224
+
225
+ //ターゲット このボタンをダイレクトにタップした時の判定は⬇️の パターン2にあるよ
226
+
227
+ saveButton.addTarget(self, action: #selector(save2(_:)), for: .touchUpInside)
228
+
229
+
230
+
231
+ view.addSubview(cancelbutton)
232
+
233
+ cancelbutton.addTarget(self, action: #selector(close(_:)), for: .touchUpInside)
234
+
235
+ }
236
+
237
+
238
+
239
+
240
+
241
+ //パターン????
242
+
243
+ //"return"をタップ キーボード閉じる
244
+
245
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
246
+
247
+
248
+
249
+ //"return"でtextFieldが閉じる(userName, place, siteなど)
250
+
251
+ textField.resignFirstResponder()
252
+
253
+
254
+
255
+ //TextFieldに入力された値に反応して、その値を習得。
256
+
257
+ NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeNotifyTextField(sender:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
258
+
259
+
260
+
261
+ return true
262
+
263
+ }
264
+
265
+
266
+
267
+ //senderの中に 変更があったUITextFieldが入ってくる感じ
268
+
269
+ @objc public func changeNotifyTextField (sender: NSNotification) {
270
+
271
+
272
+
273
+ guard let textView = sender.object as? UITextField else { return }
274
+
275
+
276
+
277
+ //文字が1以上で有効に
278
+
279
+ saveButton.isEnabled = (userName.text?.count)! > 0
280
+
281
+
282
+
283
+ //タップ処理を登録
284
+
285
+ saveButton.addTarget(self, action: #selector(save3(_:)), for: .touchUpInside)
286
+
287
+ }
288
+
289
+
290
+
291
+ //????
292
+
293
+
294
+
295
+ //パターン????
296
+
297
+ //saveButtonをダイレクトにタップ
298
+
299
+ @objc func save2(_ sender: UITapGestureRecognizer) {
300
+
301
+
302
+
303
+ NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeNotifyTextField2(sender:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
304
+
305
+ }
306
+
307
+
308
+
309
+
310
+
311
+ //senderの中に 変更があったUITextFieldが入ってくる感じ
312
+
313
+ @objc public func changeNotifyTextField2 (sender: NSNotification) {
314
+
315
+
316
+
151
- guard let containerView = containerView else {
317
+ guard let textView = sender.object as? UITextField else {
152
318
 
153
319
  return
154
320
 
155
321
  }
156
322
 
323
+ //文字が1以上で有効に
324
+
325
+ saveButton.isEnabled = (userName.text?.count)! > 0
326
+
327
+ //タップ処理を登録
328
+
329
+ saveButton.addTarget(self, action: #selector(save3(_:)), for: .touchUpInside)
330
+
331
+ }
332
+
333
+
334
+
335
+
336
+
337
+ @objc func save3(_ sender: UITapGestureRecognizer) {
338
+
157
339
 
158
340
 
341
+ //値を渡したい
342
+
159
- overlayView.frame = containerView.bounds
343
+ let controller = presentingViewController as! CollectionViewCell
160
-
161
- overlayView.backgroundColor = .black
344
+
162
-
163
- overlayView.alpha = 0.0
345
+
164
-
346
+
165
- containerView.insertSubview(overlayView, at: 0)
347
+ controller.textFromModal = userName.text!
166
-
167
-
168
-
169
- presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
348
+
170
-
171
- self?.overlayView.alpha = 0.7
349
+
172
-
350
+
173
- }, completion:nil)
351
+ self.dismiss(animated: true, completion: nil)
352
+
353
+
354
+
174
-
355
+ print("タップ キーボードとモーダルビュー 閉じたよ")
356
+
357
+
358
+
359
+ saveButton.endEditing(true)
360
+
361
+
362
+
175
- }
363
+ }
364
+
176
-
365
+ //????
177
-
178
-
366
+
179
- override func dismissalTransitionWillBegin() {
367
+ @objc func close(_ sender: UITapGestureRecognizer) {
180
-
181
- presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
368
+
182
-
183
- self?.overlayView.alpha = 0.0
369
+ print("保存キャンセル モーダルビュー 閉じたよ")
184
-
370
+
185
- }, completion:nil)
371
+ self.dismiss(animated: true, completion: nil)
186
-
372
+
187
- }
373
+ }
374
+
375
+
376
+
188
-
377
+ }
378
+
379
+
380
+
381
+
382
+
189
-
383
+ ```
384
+
385
+
386
+
190
-
387
+ ```ここに言語を入力
388
+
389
+
390
+
391
+ import UIKit
392
+
393
+
394
+
191
- override func dismissalTransitionDidEnd(_ completed: Bool) {
395
+ class CollectionViewCell: UICollectionViewCell {
396
+
397
+
398
+
192
-
399
+ //モーダルビューから受け取るテキスト
400
+
401
+ var textFromModal = "" {
402
+
193
- if completed {
403
+ didSet {
404
+
194
-
405
+ // 値が更新されたら、ラベルの内容も更新する
406
+
195
- overlayView.removeFromSuperview()
407
+ updateLabel(text: textFromModal)
196
408
 
197
409
  }
198
410
 
@@ -200,444 +412,108 @@
200
412
 
201
413
 
202
414
 
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
- }
415
+ //ラベルの内容を更新する処理
416
+
417
+ func updateLabel(text: String) {
418
+
419
+ nameLabel.text = ""
420
+
421
+ }
422
+
423
+
424
+
425
+ override init(frame: CGRect) {
426
+
427
+ super.init(frame: frame)
428
+
429
+ backgroundColor = .gray
430
+
431
+ set()
432
+
433
+ }
434
+
435
+
436
+
437
+ required init?(coder aDecoder: NSCoder) {
438
+
439
+ fatalError("init(coder:) has not been implemented")
440
+
441
+ }
442
+
443
+
444
+
445
+ let button: UIButton = {
446
+
447
+ let b = UIButton()
448
+
449
+ b.setTitle("モーダル", for: .normal)
450
+
451
+ b.backgroundColor = .blue
452
+
453
+ b.translatesAutoresizingMaskIntoConstraints = false
454
+
455
+ b.addTarget(self, action: #selector(PresentationController.openButton(_:)), for: .touchUpInside)
456
+
457
+ b.frame = CGRect(x: 20, y: 50, width: 100, height: 100)
458
+
459
+ return b
460
+
461
+ }()
462
+
463
+
464
+
465
+
466
+
467
+ let nameLabel: UILabel = {
468
+
469
+ let l = UILabel()
470
+
471
+ l.backgroundColor = .white
472
+
473
+ l.translatesAutoresizingMaskIntoConstraints = false
474
+
475
+ l.frame = CGRect(x: 20, y: 200, width: 200, height: 50)
476
+
477
+
478
+
479
+ return l
480
+
481
+ }()
482
+
483
+
484
+
485
+ func set() {
486
+
487
+ addSubview(button)
488
+
489
+ addSubview(nameLabel)
490
+
491
+ }
252
492
 
253
493
  }
254
494
 
255
495
 
256
496
 
497
+ //UIPresentationControllerで返す クラス名(CustomPresentationController)
498
+
499
+ extension PresentationController: UIViewControllerTransitioningDelegate {
500
+
501
+ func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
502
+
503
+ return CustomPresentationController(presentedViewController: presented, presenting: presenting)
504
+
505
+ }
506
+
507
+
508
+
509
+ }
510
+
511
+
512
+
257
513
  ```
258
514
 
259
515
 
260
516
 
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
-
293
- un.frame = CGRect(x: 100, y: 120, width: 200, height: 40)
294
-
295
- un.returnKeyType = .done
296
-
297
- return un
298
-
299
- }()
517
+ ・追記
300
-
301
-
302
-
303
- let saveButton: UIButton = {
518
+
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
-
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! CollectionViewCell
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 CollectionViewCell: UICollectionViewCell {
519
+ 名称をSecondViewControllerからCollectionViewCellへ変更しました。紛らわしく申し訳ありません。
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
- ```

1

変更

2018/06/19 07:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  FirstViewController(UIViewController)から
2
2
 
3
- SecondViewController(UICollectionViewCell)へ値を渡す方法をコードのみでの実現したいと思っています。
3
+ CollectionViewCell(UICollectionViewCell)へ値を渡す方法をコードのみでの実現したいと思っています。
4
4
 
5
5
 
6
6
 
@@ -46,7 +46,7 @@
46
46
 
47
47
 
48
48
 
49
- collectionView?.register(SecondViewController.self, forCellWithReuseIdentifier: "cellId")
49
+ collectionView?.register(CollectionViewCell, forCellWithReuseIdentifier: "cellId")
50
50
 
51
51
 
52
52
 
@@ -70,7 +70,7 @@
70
70
 
71
71
  default:
72
72
 
73
- return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! SecondViewController
73
+ return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CollectionViewCell
74
74
 
75
75
 
76
76
 
@@ -468,7 +468,7 @@
468
468
 
469
469
  //値を渡したい
470
470
 
471
- let controller = presentingViewController as! SecondViewController
471
+ let controller = presentingViewController as! CollectionViewCell
472
472
 
473
473
 
474
474
 
@@ -520,7 +520,7 @@
520
520
 
521
521
 
522
522
 
523
- class SecondViewController: UICollectionViewCell {
523
+ class CollectionViewCell: UICollectionViewCell {
524
524
 
525
525
 
526
526