teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

インデント修正

2017/12/07 02:33

投稿

fromageblanc
fromageblanc

スコア2724

answer CHANGED
@@ -76,13 +76,12 @@
76
76
  override func viewDidLoad() {
77
77
  super.viewDidLoad()
78
78
 
79
- // TabBarのインスタンス経由でFirstViewControllerのインスタンスを取得
79
+ // TabBarのインスタンス経由でFirstViewControllerのインスタンスを取得
80
- if let tabvc = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController {
80
+ if let tabvc = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController {
81
+ // FirstViewControllerのインスタンスをdelegateにセット
82
+ self.delegate = tabvc.viewControllers?.first as? TestDelegate
83
+ }
81
84
 
82
- // FirstViewControllerのインスタンスをdelegateにセット
83
- self.delegate = tabvc.viewControllers?.first as? TestDelegate
84
- }
85
-
86
85
  }
87
86
 
88
87
  override func didReceiveMemoryWarning() {

1

追記

2017/12/07 02:33

投稿

fromageblanc
fromageblanc

スコア2724

answer CHANGED
@@ -31,4 +31,62 @@
31
31
  }
32
32
 
33
33
  }
34
+ ```
35
+
36
+ ## 2017.12.7 追記
37
+ TabBarのインスタンス経由でFirstViewControllerのインスタンスを取得
38
+ (FirstViewControllerがイニシャルビューと仮定してます)
39
+ ```swift
40
+
41
+ // FirstViewController.swift
42
+ import UIKit
43
+
44
+ class FirstViewController: UIViewController, TestDelegate {
45
+
46
+ func test() {
47
+ print("called delegate")
48
+ }
49
+
50
+ override func viewDidLoad() {
51
+ super.viewDidLoad()
52
+
53
+ }
54
+
55
+ override func didReceiveMemoryWarning() {
56
+ super.didReceiveMemoryWarning()
57
+ }
58
+ }
59
+
60
+ // SecondViewController.swift
61
+ import UIKit
62
+
63
+ protocol TestDelegate {
64
+ func test()
65
+ }
66
+
67
+ class SecondViewController: UIViewController {
68
+
69
+ var delegate:TestDelegate?
70
+
71
+ @IBAction func pushButton(_ sender: UIButton) {
72
+ delegate?.test()
73
+
74
+ }
75
+
76
+ override func viewDidLoad() {
77
+ super.viewDidLoad()
78
+
79
+ // TabBarのインスタンス経由でFirstViewControllerのインスタンスを取得
80
+ if let tabvc = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController {
81
+
82
+ // FirstViewControllerのインスタンスをdelegateにセット
83
+ self.delegate = tabvc.viewControllers?.first as? TestDelegate
84
+ }
85
+
86
+ }
87
+
88
+ override func didReceiveMemoryWarning() {
89
+ super.didReceiveMemoryWarning()
90
+ }
91
+ }
34
92
  ```