質問編集履歴
1
説明変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,196 +10,196 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
|
14
|
+
|
15
|
+
下記のコードではsegmentを押した時はラベルが表示されますが、スワイプで切り替えた時そのラベルが消えてしまいます。というかそもそも表示されていない...?
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
ラベルが表示されるviewはsegmentを押下したときとスワイプで切り替えるときではviewが異なるんでしょか。。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
### 該当のソースコード
|
26
|
+
|
27
|
+
|
28
|
+
|
13
29
|
```
|
14
30
|
|
31
|
+
import UIKit
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
class MainViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
|
36
|
+
|
37
|
+
|
38
|
+
|
15
|
-
|
39
|
+
let idList: [String] = ["first", "second", "third"]
|
40
|
+
|
41
|
+
|
42
|
+
|
16
|
-
|
43
|
+
var pageViewController: UIPageViewController!
|
44
|
+
|
17
|
-
|
45
|
+
var viewControllers: [UIViewController] = []
|
46
|
+
|
47
|
+
|
48
|
+
|
18
|
-
|
49
|
+
@IBOutlet weak var selectTab: UISegmentedControl!
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
override func viewDidLoad() {
|
54
|
+
|
55
|
+
super.viewDidLoad()
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
selectTab.setTitle("1枚目", forSegmentAt: 0)
|
60
|
+
|
61
|
+
selectTab.setTitle("2枚目", forSegmentAt: 1)
|
62
|
+
|
63
|
+
selectTab.setTitle("3枚目", forSegmentAt: 2)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
for id in idList {
|
68
|
+
|
69
|
+
viewControllers.append((storyboard?.instantiateViewController(withIdentifier: id))!)
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
pageViewController = children[0] as? UIPageViewController
|
76
|
+
|
77
|
+
pageViewController.setViewControllers([viewControllers[0]], direction: .forward, animated: true, completion: nil)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
pageViewController.dataSource = self
|
82
|
+
|
83
|
+
pageViewController.delegate = self
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
let mylabel = UILabel(frame: CGRect(x:150, y:200, width:160, height:30))
|
88
|
+
|
89
|
+
mylabel.font = mylabel.font.withSize(25)
|
90
|
+
|
91
|
+
mylabel.textColor = UIColor.black
|
92
|
+
|
93
|
+
mylabel.text = "Test"
|
94
|
+
|
95
|
+
viewControllers[0].view.addSubview(mylabel)
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
override func didReceiveMemoryWarning() {
|
102
|
+
|
103
|
+
super.didReceiveMemoryWarning()
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
|
110
|
+
|
111
|
+
let index = idList.firstIndex(of: viewController.restorationIdentifier!)!
|
112
|
+
|
113
|
+
if (index > 0) {
|
114
|
+
|
115
|
+
print("before")
|
116
|
+
|
117
|
+
return storyboard!.instantiateViewController(withIdentifier: idList[index - 1])
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
return nil
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
|
128
|
+
|
129
|
+
let index = idList.firstIndex(of: viewController.restorationIdentifier!)!
|
130
|
+
|
131
|
+
if (index < idList.count - 1) {
|
132
|
+
|
133
|
+
print("after")
|
134
|
+
|
135
|
+
return storyboard!.instantiateViewController(withIdentifier: idList[index + 1])
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
return nil
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
|
146
|
+
|
147
|
+
let index = idList.firstIndex(of: (pageViewController.viewControllers?.first!.restorationIdentifier)!)
|
148
|
+
|
149
|
+
self.selectTab.selectedSegmentIndex = index!
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
// let mylabel = UILabel(frame: CGRect(x:150, y:200, width:160, height:30))
|
154
|
+
|
155
|
+
// //mylabel.font = mylabel.font.withSize(25)
|
156
|
+
|
157
|
+
// mylabel.textColor = UIColor.black
|
158
|
+
|
159
|
+
// mylabel.text = "Test"
|
160
|
+
|
161
|
+
// viewControllers[0].view.addSubview(mylabel)
|
162
|
+
|
163
|
+
// print(index)
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
19
|
-
|
169
|
+
@IBAction func selectedTab(_ sender: UISegmentedControl) {
|
170
|
+
|
171
|
+
switch sender.selectedSegmentIndex {
|
172
|
+
|
173
|
+
case 0:
|
174
|
+
|
175
|
+
pageViewController.setViewControllers([viewControllers[0]], direction: .reverse, animated: false, completion: nil)
|
176
|
+
|
177
|
+
break
|
178
|
+
|
179
|
+
case 1:
|
180
|
+
|
181
|
+
pageViewController.setViewControllers([viewControllers[1]], direction: .reverse, animated: false, completion: nil)
|
182
|
+
|
183
|
+
break
|
184
|
+
|
185
|
+
case 2:
|
186
|
+
|
187
|
+
pageViewController.setViewControllers([viewControllers[2]], direction: .forward, animated: false, completion: nil)
|
188
|
+
|
189
|
+
break
|
190
|
+
|
191
|
+
default:
|
192
|
+
|
193
|
+
return
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
}
|
20
202
|
|
21
203
|
```
|
22
204
|
|
23
|
-
|
24
|
-
|
25
|
-
### 該当のソースコード
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
```
|
30
|
-
|
31
|
-
import UIKit
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
class MainViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
let idList: [String] = ["first", "second", "third"]
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
var pageViewController: UIPageViewController!
|
44
|
-
|
45
|
-
var viewControllers: [UIViewController] = []
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
@IBOutlet weak var selectTab: UISegmentedControl!
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
override func viewDidLoad() {
|
54
|
-
|
55
|
-
super.viewDidLoad()
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
selectTab.setTitle("1枚目", forSegmentAt: 0)
|
60
|
-
|
61
|
-
selectTab.setTitle("2枚目", forSegmentAt: 1)
|
62
|
-
|
63
|
-
selectTab.setTitle("3枚目", forSegmentAt: 2)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
for id in idList {
|
68
|
-
|
69
|
-
viewControllers.append((storyboard?.instantiateViewController(withIdentifier: id))!)
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
pageViewController = children[0] as? UIPageViewController
|
76
|
-
|
77
|
-
pageViewController.setViewControllers([viewControllers[0]], direction: .forward, animated: true, completion: nil)
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
pageViewController.dataSource = self
|
82
|
-
|
83
|
-
pageViewController.delegate = self
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
let mylabel = UILabel(frame: CGRect(x:150, y:200, width:160, height:30))
|
88
|
-
|
89
|
-
mylabel.font = mylabel.font.withSize(25)
|
90
|
-
|
91
|
-
mylabel.textColor = UIColor.black
|
92
|
-
|
93
|
-
mylabel.text = "Test"
|
94
|
-
|
95
|
-
viewControllers[0].view.addSubview(mylabel)
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
override func didReceiveMemoryWarning() {
|
102
|
-
|
103
|
-
super.didReceiveMemoryWarning()
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
|
110
|
-
|
111
|
-
let index = idList.firstIndex(of: viewController.restorationIdentifier!)!
|
112
|
-
|
113
|
-
if (index > 0) {
|
114
|
-
|
115
|
-
print("before")
|
116
|
-
|
117
|
-
return storyboard!.instantiateViewController(withIdentifier: idList[index - 1])
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
return nil
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
|
128
|
-
|
129
|
-
let index = idList.firstIndex(of: viewController.restorationIdentifier!)!
|
130
|
-
|
131
|
-
if (index < idList.count - 1) {
|
132
|
-
|
133
|
-
print("after")
|
134
|
-
|
135
|
-
return storyboard!.instantiateViewController(withIdentifier: idList[index + 1])
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
return nil
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
|
146
|
-
|
147
|
-
let index = idList.firstIndex(of: (pageViewController.viewControllers?.first!.restorationIdentifier)!)
|
148
|
-
|
149
|
-
self.selectTab.selectedSegmentIndex = index!
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
// let mylabel = UILabel(frame: CGRect(x:150, y:200, width:160, height:30))
|
154
|
-
|
155
|
-
// //mylabel.font = mylabel.font.withSize(25)
|
156
|
-
|
157
|
-
// mylabel.textColor = UIColor.black
|
158
|
-
|
159
|
-
// mylabel.text = "Test"
|
160
|
-
|
161
|
-
// viewControllers[0].view.addSubview(mylabel)
|
162
|
-
|
163
|
-
// print(index)
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
@IBAction func selectedTab(_ sender: UISegmentedControl) {
|
170
|
-
|
171
|
-
switch sender.selectedSegmentIndex {
|
172
|
-
|
173
|
-
case 0:
|
174
|
-
|
175
|
-
pageViewController.setViewControllers([viewControllers[0]], direction: .reverse, animated: false, completion: nil)
|
176
|
-
|
177
|
-
break
|
178
|
-
|
179
|
-
case 1:
|
180
|
-
|
181
|
-
pageViewController.setViewControllers([viewControllers[1]], direction: .reverse, animated: false, completion: nil)
|
182
|
-
|
183
|
-
break
|
184
|
-
|
185
|
-
case 2:
|
186
|
-
|
187
|
-
pageViewController.setViewControllers([viewControllers[2]], direction: .forward, animated: false, completion: nil)
|
188
|
-
|
189
|
-
break
|
190
|
-
|
191
|
-
default:
|
192
|
-
|
193
|
-
return
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
```
|
204
|
-
|
205
205
|
![イメージ説明](6beb54f086f160eb1dc893a6199b9300.png)
|