回答編集履歴

2

回答を追記しました。

2018/03/06 23:31

投稿

newmt
newmt

スコア1277

test CHANGED
@@ -20,12 +20,164 @@
20
20
 
21
21
  【追記】
22
22
 
23
- 下記のsetViewControllersにgetChat()を追記したらどうでしょうか?
23
+ ~~下記のsetViewControllersにgetChat()を追記したらどうでしょうか?~~
24
24
 
25
25
 
26
26
 
27
- ```Swift
27
+ ~~self.setViewControllers([getFlow(), getChat()], direction: .forward, animated: true, completion: nil)
28
28
 
29
+ ~~
30
+
31
+
32
+
33
+ 【追記2】
34
+
35
+ すいません。↑は別のものと勘違いしていました。
36
+
37
+ ご質問の追記を見て色々試してみたのですが中々うまくいかず、上記とは少し違う形で一応segmentedControlを使って画面遷移する方法はあるのですが、これでやりたいことが達成されますでしょうか?
38
+
39
+ 違っていたらごめんなさい。
40
+
41
+
42
+
43
+ ![イメージ説明](f439c234fc41e5a77d31b4cf9a2ebbc0.png)
44
+
45
+
46
+
47
+ ```ViewController.swift
48
+
49
+
50
+
51
+ import UIKit
52
+
53
+
54
+
55
+ class ViewController: UIViewController {
56
+
57
+
58
+
59
+ @IBOutlet weak var segmentedControl: UISegmentedControl!
60
+
61
+ override func viewDidLoad() {
62
+
63
+ super.viewDidLoad()
64
+
65
+ }
66
+
67
+
68
+
69
+ override func viewDidAppear(_ animated: Bool) {
70
+
71
+ super.viewDidAppear(animated)
72
+
73
+ changeToFlowController()
74
+
75
+ }
76
+
77
+ func getFlow() -> FlowViewController {
78
+
79
+
80
+
81
+ let storyboard = UIStoryboard(name: "Main", bundle: nil)
82
+
29
- self.setViewControllers([getFlow(), getChat()], direction: .forward, animated: true, completion: nil)
83
+ return storyboard.instantiateViewController(withIdentifier: "flow") as! FlowViewController
84
+
85
+ }
86
+
87
+
88
+
89
+ func getChat() -> ChatViewController {
90
+
91
+ let storyboard = UIStoryboard(name: "Main", bundle: nil)
92
+
93
+ return storyboard.instantiateViewController(withIdentifier: "chat") as! ChatViewController
94
+
95
+ }
96
+
97
+
98
+
99
+ @IBAction func changePage(_ sender: UISegmentedControl) {
100
+
101
+
102
+
103
+ switch sender.selectedSegmentIndex {
104
+
105
+ case 0:
106
+
107
+ changeToFlowController()
108
+
109
+ case 1:
110
+
111
+ changeToChatController()
112
+
113
+ default:
114
+
115
+ changeToFlowController()
116
+
117
+ }
118
+
119
+ }
120
+
121
+
122
+
123
+ func changeToFlowController() {
124
+
125
+
126
+
127
+ let vc = getFlow()
128
+
129
+ removeLast()
130
+
131
+ add(vc)
132
+
133
+
134
+
135
+ }
136
+
137
+
138
+
139
+ func changeToChatController() {
140
+
141
+ let vc = getChat()
142
+
143
+ removeLast()
144
+
145
+ add(vc)
146
+
147
+ }
148
+
149
+
150
+
151
+
152
+
153
+ func add(_ childController: UIViewController) {
154
+
155
+ addChildViewController(childController)
156
+
157
+ view.addSubview(childController.view)
158
+
159
+ childController.didMove(toParentViewController: self)
160
+
161
+ view.bringSubview(toFront: segmentedControl)
162
+
163
+ }
164
+
165
+
166
+
167
+ func removeLast() {
168
+
169
+ guard let viewController = self.childViewControllers.last else {
170
+
171
+ return
172
+
173
+ }
174
+
175
+ viewController.view.removeFromSuperview()
176
+
177
+ viewController.removeFromParentViewController()
178
+
179
+ }
180
+
181
+ }
30
182
 
31
183
  ```

1

回答に追記しました。

2018/03/06 23:31

投稿

newmt
newmt

スコア1277

test CHANGED
@@ -15,3 +15,17 @@
15
15
 
16
16
 
17
17
  ```
18
+
19
+
20
+
21
+ 【追記】
22
+
23
+ 下記のsetViewControllersにgetChat()を追記したらどうでしょうか?
24
+
25
+
26
+
27
+ ```Swift
28
+
29
+ self.setViewControllers([getFlow(), getChat()], direction: .forward, animated: true, completion: nil)
30
+
31
+ ```