一つのViewController
上に2つのPageViewController
を設置したいのですが、
setPageViewController()
/setViewControllers(_:direction:animated:completion:)
で設置するPageViewController
を選ぶことができません(適当な方へ設置される)。
どちらか指定する方法をご存知でしたら教えて下さい。
tableView
などではOutlet接続
して名前をつけることによって指定できますが、pageViewController
の名前の付け方がわかりませんでした。
↓このような感じのコードで設置しようとしています。
swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 var upperPageViewController: UIPageViewController! 6 var lowerPageViewController: UIPageViewController! 7 8 var upperPageViewArray: [String] = ["A", "B", "C", "D", "E"] 9 var lowerPageViewArray: [String] = ["あ", "か", "さ", "た", "な"] 10 11 var upperPageViewIndex: Int = 0 12 var lowerPageViewIndex: Int = 0 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // pageViewControllerの設定 -> prepareへ 17 // pageControlの設定 18 setUpperPageViewController() 19 setLowerPageViewController() 20 21 upperPageViewController?.delegate = self 22 upperPageViewController?.dataSource = self 23 lowerPageViewController?.delegate = self 24 lowerPageViewController?.dataSource = self 25 } 26 27 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 28 switch segue.identifier { 29 case "EmbedUpperPVC": 30 upperPageViewController = (segue.destination as! UIPageViewController) 31 case "EmbedLowerPVC": 32 lowerPageViewController = (segue.destination as! UIPageViewController) 33 default: fatalError("prepare") 34 } 35 } 36 37} 38 39 40 41extension ViewController: UIPageViewControllerDelegate { 42 43 func pageViewController(_ pageViewController: UIPageViewController, 44 didFinishAnimating finished: Bool, 45 previousViewControllers: [UIViewController], 46 transitionCompleted completed: Bool) 47 { 48 49 if completed { 50 switch pageViewController { 51 case upperPageViewController: 52 guard let vc = upperPageViewController?.viewControllers?.first as? ContentsVC else { return } 53 let index = vc.dataSource.index 54 self.upperPageViewIndex = index 55 case lowerPageViewController: 56 guard let vc = lowerPageViewController?.viewControllers?.first as? ContentsVC else { return } 57 let index = vc.dataSource.index 58 self.lowerPageViewIndex = index 59 default: fatalError("pageViewController/didFinishAnimating/fatalError") 60 } 61 } 62 } 63} 64 65extension ViewController: UIPageViewControllerDataSource { 66 67 func pageViewController(_ pageViewController: UIPageViewController, 68 viewControllerBefore viewController: UIViewController) -> UIViewController? 69 { 70 var index: Int 71 let str: String 72 73 switch pageViewController { 74 case upperPageViewController: 75 index = upperPageViewIndex 76 if index == 0 { return nil } 77 index -= 1 78 str = upperPageViewArray[index] 79 case lowerPageViewController: 80 index = lowerPageViewIndex 81 if index == 0 { return nil } 82 index -= 1 83 str = lowerPageViewArray[index] 84 default: fatalError("viewControllerBefore/fatalError") 85 } 86 guard let vc = createContentsVC(index: index, str: str) else { return nil } 87 88 return vc 89 } 90 91 func pageViewController(_ pageViewController: UIPageViewController, 92 viewControllerAfter viewController: UIViewController) -> UIViewController? 93 { 94 var index: Int 95 let str: String 96 97 switch pageViewController { 98 case upperPageViewController: 99 index = upperPageViewIndex 100 if index >= upperPageViewArray.count - 1 { return nil } 101 index += 1 102 str = upperPageViewArray[index] 103 case lowerPageViewController: 104 index = lowerPageViewIndex 105 if index >= lowerPageViewArray.count - 1 { return nil } 106 index += 1 107 str = lowerPageViewArray[index] 108 default: fatalError("viewControllerBefore/fatalError") 109 } 110 guard let vc = createContentsVC(index: index, str: str) else { return nil } 111 112 return vc 113 } 114 115} 116 117// setPageViewController 118extension ViewController { 119 120 func setUpperPageViewController(direction: UIPageViewController.NavigationDirection = .forward) { 121 122 let index = upperPageViewIndex 123 let str = upperPageViewArray[index] 124 125 guard let vc = createContentsVC(index: index, str: str) else { return } 126 127 self.upperPageViewController?.setViewControllers([vc], direction: .forward, animated: false, completion: nil) 128 } 129 130 func setLowerPageViewController(direction: UIPageViewController.NavigationDirection = .forward) { 131 132 let index = lowerPageViewIndex 133 let str = lowerPageViewArray[index] 134 135 guard let vc = createContentsVC(index: index, str: str) else { return } 136 137 self.lowerPageViewController?.setViewControllers([vc], direction: .forward, animated: false, completion: nil) 138 } 139 140 func createContentsVC(index: Int, str: String) -> UIViewController? { 141 142 let sb = UIStoryboard(name: "Main", bundle: nil) 143 let vc = sb.instantiateViewController(withIdentifier: "ContentsVC") as! ContentsVC 144 vc.dataSource = ContentsVCDataStore.init(index: index, str: str) 145 146 return vc 147 } 148 149} 150 151 152class ContentsVC: UIViewController { 153 154 var dataSource: ContentsVCDataStore! 155 156 @IBOutlet weak var tLabel: UILabel! 157 158 override func viewDidLoad() { 159 super.viewDidLoad() 160 tLabel.text = dataSource.str 161 } 162} 163 164class ContentsVCDataStore { 165 166 var index: Int 167 var str: String 168 169 init(index: Int, str: String) { 170 self.index = index 171 self.str = str 172 } 173} 174
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。