質問編集履歴
8
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
どちらか指定する方法をご存知でしたら教えて下さい。
|
4
4
|
`tableView`などでは`Outlet接続`して名前をつけることによって指定できますが、`pageViewController`の名前の付け方がわかりませんでした。
|
5
5
|
|
6
|
-

|
7
7
|
|
8
8
|
[サンプル](https://github.com/tyobigoro/tPageViewController)
|
9
9
|
|
@@ -14,8 +14,8 @@
|
|
14
14
|
|
15
15
|
class ViewController: UIViewController {
|
16
16
|
|
17
|
-
var upperPageViewController:
|
17
|
+
var upperPageViewController: UIPageViewController!
|
18
|
-
var lowerPageViewController:
|
18
|
+
var lowerPageViewController: UIPageViewController!
|
19
19
|
|
20
20
|
var upperPageViewArray: [String] = ["A", "B", "C", "D", "E"]
|
21
21
|
var lowerPageViewArray: [String] = ["あ", "か", "さ", "た", "な"]
|
@@ -39,9 +39,9 @@
|
|
39
39
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
40
40
|
switch segue.identifier {
|
41
41
|
case "EmbedUpperPVC":
|
42
|
-
upperPageViewController = (segue.destination as!
|
42
|
+
upperPageViewController = (segue.destination as! UIPageViewController)
|
43
43
|
case "EmbedLowerPVC":
|
44
|
-
lowerPageViewController = (segue.destination as!
|
44
|
+
lowerPageViewController = (segue.destination as! UIPageViewController)
|
45
45
|
default: fatalError("prepare")
|
46
46
|
}
|
47
47
|
}
|
@@ -184,10 +184,4 @@
|
|
184
184
|
}
|
185
185
|
}
|
186
186
|
|
187
|
-
|
188
|
-
class UpperPageViewController: UIPageViewController {}
|
189
|
-
|
190
|
-
class LowerPageViewController: UIPageViewController {}
|
191
|
-
|
192
|
-
|
193
187
|
```
|
7
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,14 +25,11 @@
|
|
25
25
|
|
26
26
|
override func viewDidLoad() {
|
27
27
|
super.viewDidLoad()
|
28
|
-
|
29
28
|
// pageViewControllerの設定 -> prepareへ
|
30
|
-
|
31
29
|
// pageControlの設定
|
32
30
|
setUpperPageViewController()
|
33
31
|
setLowerPageViewController()
|
34
|
-
|
35
|
-
|
32
|
+
|
36
33
|
upperPageViewController?.delegate = self
|
37
34
|
upperPageViewController?.dataSource = self
|
38
35
|
lowerPageViewController?.delegate = self
|
@@ -62,7 +59,7 @@
|
|
62
59
|
{
|
63
60
|
|
64
61
|
if completed {
|
65
|
-
|
62
|
+
switch pageViewController {
|
66
63
|
case upperPageViewController:
|
67
64
|
guard let vc = upperPageViewController?.viewControllers?.first as? ContentsVC else { return }
|
68
65
|
let index = vc.dataSource.index
|
@@ -71,7 +68,7 @@
|
|
71
68
|
guard let vc = lowerPageViewController?.viewControllers?.first as? ContentsVC else { return }
|
72
69
|
let index = vc.dataSource.index
|
73
70
|
self.lowerPageViewIndex = index
|
74
|
-
|
71
|
+
default: fatalError("pageViewController/didFinishAnimating/fatalError")
|
75
72
|
}
|
76
73
|
}
|
77
74
|
}
|
@@ -91,16 +88,13 @@
|
|
91
88
|
if index == 0 { return nil }
|
92
89
|
index -= 1
|
93
90
|
str = upperPageViewArray[index]
|
94
|
-
|
95
91
|
case lowerPageViewController:
|
96
|
-
index =
|
92
|
+
index = lowerPageViewIndex
|
97
93
|
if index == 0 { return nil }
|
98
94
|
index -= 1
|
99
|
-
str =
|
95
|
+
str = lowerPageViewArray[index]
|
100
|
-
|
101
96
|
default: fatalError("viewControllerBefore/fatalError")
|
102
97
|
}
|
103
|
-
|
104
98
|
guard let vc = createContentsVC(index: index, str: str) else { return nil }
|
105
99
|
|
106
100
|
return vc
|
@@ -118,16 +112,13 @@
|
|
118
112
|
if index >= upperPageViewArray.count - 1 { return nil }
|
119
113
|
index += 1
|
120
114
|
str = upperPageViewArray[index]
|
121
|
-
|
122
115
|
case lowerPageViewController:
|
123
|
-
index =
|
116
|
+
index = lowerPageViewIndex
|
124
117
|
if index >= lowerPageViewArray.count - 1 { return nil }
|
125
118
|
index += 1
|
126
|
-
str =
|
119
|
+
str = lowerPageViewArray[index]
|
127
|
-
|
128
120
|
default: fatalError("viewControllerBefore/fatalError")
|
129
121
|
}
|
130
|
-
|
131
122
|
guard let vc = createContentsVC(index: index, str: str) else { return nil }
|
132
123
|
|
133
124
|
return vc
|
@@ -155,8 +146,6 @@
|
|
155
146
|
|
156
147
|
guard let vc = createContentsVC(index: index, str: str) else { return }
|
157
148
|
|
158
|
-
print("setLowerPageVC:",vc)
|
159
|
-
|
160
149
|
self.lowerPageViewController?.setViewControllers([vc], direction: .forward, animated: false, completion: nil)
|
161
150
|
}
|
162
151
|
|
@@ -200,4 +189,5 @@
|
|
200
189
|
|
201
190
|
class LowerPageViewController: UIPageViewController {}
|
202
191
|
|
192
|
+
|
203
193
|
```
|
6
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,8 +14,8 @@
|
|
14
14
|
|
15
15
|
class ViewController: UIViewController {
|
16
16
|
|
17
|
-
var upperPageViewController: UpperPageViewController
|
17
|
+
var upperPageViewController: UpperPageViewController!
|
18
|
-
var lowerPageViewController: LowerPageViewController
|
18
|
+
var lowerPageViewController: LowerPageViewController!
|
19
19
|
|
20
20
|
var upperPageViewArray: [String] = ["A", "B", "C", "D", "E"]
|
21
21
|
var lowerPageViewArray: [String] = ["あ", "か", "さ", "た", "な"]
|
@@ -26,12 +26,11 @@
|
|
26
26
|
override func viewDidLoad() {
|
27
27
|
super.viewDidLoad()
|
28
28
|
|
29
|
-
// pageViewControllerの設定
|
29
|
+
// pageViewControllerの設定 -> prepareへ
|
30
|
-
|
30
|
+
|
31
|
-
lowerPageViewController = children.first as? LowerPageViewController
|
32
31
|
// pageControlの設定
|
32
|
+
setUpperPageViewController()
|
33
33
|
setLowerPageViewController()
|
34
|
-
setUpperPageViewController()
|
35
34
|
|
36
35
|
|
37
36
|
upperPageViewController?.delegate = self
|
@@ -39,6 +38,17 @@
|
|
39
38
|
lowerPageViewController?.delegate = self
|
40
39
|
lowerPageViewController?.dataSource = self
|
41
40
|
}
|
41
|
+
|
42
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
43
|
+
switch segue.identifier {
|
44
|
+
case "EmbedUpperPVC":
|
45
|
+
upperPageViewController = (segue.destination as! UpperPageViewController)
|
46
|
+
case "EmbedLowerPVC":
|
47
|
+
lowerPageViewController = (segue.destination as! LowerPageViewController)
|
48
|
+
default: fatalError("prepare")
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
42
52
|
}
|
43
53
|
|
44
54
|
|
@@ -145,6 +155,8 @@
|
|
145
155
|
|
146
156
|
guard let vc = createContentsVC(index: index, str: str) else { return }
|
147
157
|
|
158
|
+
print("setLowerPageVC:",vc)
|
159
|
+
|
148
160
|
self.lowerPageViewController?.setViewControllers([vc], direction: .forward, animated: false, completion: nil)
|
149
161
|
}
|
150
162
|
|
@@ -187,4 +199,5 @@
|
|
187
199
|
class UpperPageViewController: UIPageViewController {}
|
188
200
|
|
189
201
|
class LowerPageViewController: UIPageViewController {}
|
202
|
+
|
190
203
|
```
|
5
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,80 +3,188 @@
|
|
3
3
|
どちらか指定する方法をご存知でしたら教えて下さい。
|
4
4
|
`tableView`などでは`Outlet接続`して名前をつけることによって指定できますが、`pageViewController`の名前の付け方がわかりませんでした。
|
5
5
|
|
6
|
-

|
7
7
|
|
8
|
+
[サンプル](https://github.com/tyobigoro/tPageViewController)
|
9
|
+
|
8
10
|
↓このような感じのコードで設置しようとしています。
|
9
11
|
|
10
12
|
```swift
|
11
13
|
import UIKit
|
12
14
|
|
13
15
|
class ViewController: UIViewController {
|
14
|
-
// 一応PageViewControllerを保持する変数には名前を付けて区別しているが
|
15
|
-
// Storyboard上のどっちのPageViewControllerか指定ができない
|
16
|
-
var upperPageViewController: UIPageViewController?
|
17
|
-
var lowerPageViewController: UIPageViewController?
|
18
16
|
|
17
|
+
var upperPageViewController: UpperPageViewController?
|
18
|
+
var lowerPageViewController: LowerPageViewController?
|
19
|
+
|
20
|
+
var upperPageViewArray: [String] = ["A", "B", "C", "D", "E"]
|
21
|
+
var lowerPageViewArray: [String] = ["あ", "か", "さ", "た", "な"]
|
22
|
+
|
23
|
+
var upperPageViewIndex: Int = 0
|
24
|
+
var lowerPageViewIndex: Int = 0
|
25
|
+
|
19
26
|
override func viewDidLoad() {
|
20
27
|
super.viewDidLoad()
|
21
28
|
|
22
29
|
// pageViewControllerの設定
|
23
|
-
upperPageViewController = children.first as?
|
30
|
+
upperPageViewController = children.first as? UpperPageViewController
|
24
|
-
lowerPageViewController
|
31
|
+
lowerPageViewController = children.first as? LowerPageViewController
|
25
32
|
// pageControlの設定
|
33
|
+
setLowerPageViewController()
|
26
34
|
setUpperPageViewController()
|
27
|
-
setLowerPageViewController()
|
28
35
|
|
36
|
+
|
29
37
|
upperPageViewController?.delegate = self
|
30
38
|
upperPageViewController?.dataSource = self
|
31
39
|
lowerPageViewController?.delegate = self
|
32
40
|
lowerPageViewController?.dataSource = self
|
33
41
|
}
|
42
|
+
}
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
extension ViewController: UIPageViewControllerDelegate {
|
34
47
|
|
48
|
+
func pageViewController(_ pageViewController: UIPageViewController,
|
49
|
+
didFinishAnimating finished: Bool,
|
50
|
+
previousViewControllers: [UIViewController],
|
51
|
+
transitionCompleted completed: Bool)
|
52
|
+
{
|
53
|
+
|
54
|
+
if completed {
|
55
|
+
switch pageViewController {
|
56
|
+
case upperPageViewController:
|
57
|
+
guard let vc = upperPageViewController?.viewControllers?.first as? ContentsVC else { return }
|
58
|
+
let index = vc.dataSource.index
|
59
|
+
self.upperPageViewIndex = index
|
60
|
+
case lowerPageViewController:
|
61
|
+
guard let vc = lowerPageViewController?.viewControllers?.first as? ContentsVC else { return }
|
62
|
+
let index = vc.dataSource.index
|
63
|
+
self.lowerPageViewIndex = index
|
64
|
+
default: fatalError("pageViewController/didFinishAnimating/fatalError")
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
extension ViewController: UIPageViewControllerDataSource {
|
35
71
|
|
72
|
+
func pageViewController(_ pageViewController: UIPageViewController,
|
73
|
+
viewControllerBefore viewController: UIViewController) -> UIViewController?
|
74
|
+
{
|
75
|
+
var index: Int
|
76
|
+
let str: String
|
77
|
+
|
78
|
+
switch pageViewController {
|
79
|
+
case upperPageViewController:
|
80
|
+
index = upperPageViewIndex
|
81
|
+
if index == 0 { return nil }
|
82
|
+
index -= 1
|
83
|
+
str = upperPageViewArray[index]
|
84
|
+
|
85
|
+
case lowerPageViewController:
|
86
|
+
index = upperPageViewIndex
|
87
|
+
if index == 0 { return nil }
|
88
|
+
index -= 1
|
89
|
+
str = upperPageViewArray[index]
|
90
|
+
|
91
|
+
default: fatalError("viewControllerBefore/fatalError")
|
92
|
+
}
|
93
|
+
|
94
|
+
guard let vc = createContentsVC(index: index, str: str) else { return nil }
|
95
|
+
|
96
|
+
return vc
|
97
|
+
}
|
98
|
+
|
99
|
+
func pageViewController(_ pageViewController: UIPageViewController,
|
100
|
+
viewControllerAfter viewController: UIViewController) -> UIViewController?
|
101
|
+
{
|
102
|
+
var index: Int
|
103
|
+
let str: String
|
104
|
+
|
105
|
+
switch pageViewController {
|
106
|
+
case upperPageViewController:
|
107
|
+
index = upperPageViewIndex
|
108
|
+
if index >= upperPageViewArray.count - 1 { return nil }
|
109
|
+
index += 1
|
110
|
+
str = upperPageViewArray[index]
|
111
|
+
|
112
|
+
case lowerPageViewController:
|
113
|
+
index = upperPageViewIndex
|
114
|
+
if index >= lowerPageViewArray.count - 1 { return nil }
|
115
|
+
index += 1
|
116
|
+
str = upperPageViewArray[index]
|
117
|
+
|
118
|
+
default: fatalError("viewControllerBefore/fatalError")
|
119
|
+
}
|
120
|
+
|
121
|
+
guard let vc = createContentsVC(index: index, str: str) else { return nil }
|
122
|
+
|
123
|
+
return vc
|
124
|
+
}
|
125
|
+
|
126
|
+
}
|
127
|
+
|
128
|
+
// setPageViewController
|
129
|
+
extension ViewController {
|
130
|
+
|
36
131
|
func setUpperPageViewController(direction: UIPageViewController.NavigationDirection = .forward) {
|
132
|
+
|
133
|
+
let index = upperPageViewIndex
|
134
|
+
let str = upperPageViewArray[index]
|
135
|
+
|
37
|
-
|
136
|
+
guard let vc = createContentsVC(index: index, str: str) else { return }
|
38
|
-
|
137
|
+
|
39
|
-
|
138
|
+
self.upperPageViewController?.setViewControllers([vc], direction: .forward, animated: false, completion: nil)
|
40
139
|
}
|
41
140
|
|
42
|
-
func
|
141
|
+
func setLowerPageViewController(direction: UIPageViewController.NavigationDirection = .forward) {
|
142
|
+
|
143
|
+
let index = lowerPageViewIndex
|
144
|
+
let str = lowerPageViewArray[index]
|
145
|
+
|
43
|
-
|
146
|
+
guard let vc = createContentsVC(index: index, str: str) else { return }
|
44
|
-
|
147
|
+
|
45
|
-
|
148
|
+
self.lowerPageViewController?.setViewControllers([vc], direction: .forward, animated: false, completion: nil)
|
46
149
|
}
|
47
150
|
|
151
|
+
func createContentsVC(index: Int, str: String) -> UIViewController? {
|
152
|
+
|
153
|
+
let sb = UIStoryboard(name: "Main", bundle: nil)
|
154
|
+
let vc = sb.instantiateViewController(withIdentifier: "ContentsVC") as! ContentsVC
|
155
|
+
vc.dataSource = ContentsVCDataStore.init(index: index, str: str)
|
156
|
+
|
157
|
+
return vc
|
158
|
+
}
|
159
|
+
|
48
160
|
}
|
49
161
|
|
50
|
-
```
|
51
162
|
|
163
|
+
class ContentsVC: UIViewController {
|
164
|
+
|
165
|
+
var dataSource: ContentsVCDataStore!
|
166
|
+
|
167
|
+
@IBOutlet weak var tLabel: UILabel!
|
168
|
+
|
169
|
+
override func viewDidLoad() {
|
170
|
+
super.viewDidLoad()
|
171
|
+
tLabel.text = dataSource.str
|
172
|
+
}
|
173
|
+
}
|
52
174
|
|
53
|
-
```swift
|
54
|
-
// サブクラスを作る
|
55
|
-
class
|
175
|
+
class ContentsVCDataStore {
|
176
|
+
|
177
|
+
var index: Int
|
178
|
+
var str: String
|
179
|
+
|
180
|
+
init(index: Int, str: String) {
|
181
|
+
self.index = index
|
182
|
+
self.str = str
|
183
|
+
}
|
184
|
+
}
|
56
185
|
|
57
|
-
class LowerPageViewController: UIPageViewController {}
|
58
186
|
|
59
|
-
class ViewController: UIViewController {
|
60
|
-
// 格納する変数を宣言する
|
61
|
-
// Storyboard上のどっちのPageViewControllerか指定ができない
|
62
|
-
|
187
|
+
class UpperPageViewController: UIPageViewController {}
|
63
|
-
var lowerPageViewController: LowerPageViewController?
|
64
188
|
|
65
|
-
override func viewDidLoad() {
|
66
|
-
super.viewDidLoad()
|
67
|
-
|
68
|
-
// pageViewControllerの設定
|
69
|
-
upperPageViewController = children.first as? UpperPageViewcontroller
|
70
|
-
|
189
|
+
class LowerPageViewController: UIPageViewController {}
|
71
|
-
// pageControlの設定
|
72
|
-
setUpperPageViewController()
|
73
|
-
setLowerPageViewController()
|
74
|
-
|
75
|
-
upperPageViewController?.delegate = self
|
76
|
-
upperPageViewController?.dataSource = self
|
77
|
-
lowerPageViewController?.delegate = self
|
78
|
-
lowerPageViewController?.dataSource = self
|
79
|
-
}
|
80
|
-
|
81
|
-
```
|
190
|
+
```
|
82
|
-
サブクラスを作って、それぞれの変数に適用して、storyboard上の各PageViewControllerにサブクラスを設定すると、setViewControllers(_:direction:animated:completion:)でpageViewControllerの設定先が指定どおりにできたのですが、どちらかのPageViewControllerしか描写されない状態です。
|
4
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -79,4 +79,4 @@
|
|
79
79
|
}
|
80
80
|
|
81
81
|
```
|
82
|
-
サブクラスを作って、それぞれの変数に適用して、storyboard上の各PageViewControllerにサブクラスを設定すると、setViewControllers(_:direction:animated:completion:)でpageViewControllerの設定先が指定どおりにできたのですが、
|
82
|
+
サブクラスを作って、それぞれの変数に適用して、storyboard上の各PageViewControllerにサブクラスを設定すると、setViewControllers(_:direction:animated:completion:)でpageViewControllerの設定先が指定どおりにできたのですが、どちらかのPageViewControllerしか描写されない状態です。
|
3
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -79,4 +79,4 @@
|
|
79
79
|
}
|
80
80
|
|
81
81
|
```
|
82
|
-
サブクラスを作って、それぞれの変数に適用すると、setViewControllers(_:direction:animated:completion:)でpageViewControllerの設定先が指定どおりにできたのですが、この方法で今後きちんと動くかわからないので、ご存知でしたら教えて下さい。
|
82
|
+
サブクラスを作って、それぞれの変数に適用して、storyboard上の各PageViewControllerにサブクラスを設定すると、setViewControllers(_:direction:animated:completion:)でpageViewControllerの設定先が指定どおりにできたのですが、この方法で今後きちんと動くかわからないので、ご存知でしたら教えて下さい。
|
2
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
一つの`ViewController`上に2つの`PageViewController`を設置したいのですが、
|
2
|
-
`setPageViewController()`で設置する`PageViewController`を選ぶことができません(適当な方へ設置される)。
|
2
|
+
`setPageViewController()`/`setViewControllers(_:direction:animated:completion:)`で設置する`PageViewController`を選ぶことができません(適当な方へ設置される)。
|
3
3
|
どちらか指定する方法をご存知でしたら教えて下さい。
|
4
4
|
`tableView`などでは`Outlet接続`して名前をつけることによって指定できますが、`pageViewController`の名前の付け方がわかりませんでした。
|
5
5
|
|
1
訂正
title
CHANGED
File without changes
|
body
CHANGED
@@ -47,4 +47,36 @@
|
|
47
47
|
|
48
48
|
}
|
49
49
|
|
50
|
-
```
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
```swift
|
54
|
+
// サブクラスを作る
|
55
|
+
class UpperPageViewcontroller: UIPageViewController {}
|
56
|
+
|
57
|
+
class LowerPageViewController: UIPageViewController {}
|
58
|
+
|
59
|
+
class ViewController: UIViewController {
|
60
|
+
// 格納する変数を宣言する
|
61
|
+
// Storyboard上のどっちのPageViewControllerか指定ができない
|
62
|
+
var upperPageViewController: UpperPageViewcontroller?
|
63
|
+
var lowerPageViewController: LowerPageViewController?
|
64
|
+
|
65
|
+
override func viewDidLoad() {
|
66
|
+
super.viewDidLoad()
|
67
|
+
|
68
|
+
// pageViewControllerの設定
|
69
|
+
upperPageViewController = children.first as? UpperPageViewcontroller
|
70
|
+
lowerPageViewController = children.first as? LowerPageViewController
|
71
|
+
// pageControlの設定
|
72
|
+
setUpperPageViewController()
|
73
|
+
setLowerPageViewController()
|
74
|
+
|
75
|
+
upperPageViewController?.delegate = self
|
76
|
+
upperPageViewController?.dataSource = self
|
77
|
+
lowerPageViewController?.delegate = self
|
78
|
+
lowerPageViewController?.dataSource = self
|
79
|
+
}
|
80
|
+
|
81
|
+
```
|
82
|
+
サブクラスを作って、それぞれの変数に適用すると、setViewControllers(_:direction:animated:completion:)でpageViewControllerの設定先が指定どおりにできたのですが、この方法で今後きちんと動くかわからないので、ご存知でしたら教えて下さい。
|