以下の様にstoryboardで作ったpageviewcontollerをsegmentedcontrolでも遷移する様にしたいです。
以下がsegmentedcontrolがある画面のクラス
swift
1import UIKit 2 3class SegViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 } 10 11 @IBAction func changePage(_ sender: UISegmentedControl) { 12 13 let vc = ViewController() 14 15 switch sender.selectedSegmentIndex { 16 case 0: 17 vc.getFlow() 18 case 1: 19 vc.getChat() 20 default: 21 vc.getFlow() 22 } 23 24 } 25 26 override func didReceiveMemoryWarning() { 27 super.didReceiveMemoryWarning() 28 // Dispose of any resources that can be recreated. 29 } 30 31 32 33 34}
以下がpageviewcontrollerのクラスです。
swift
1import UIKit 2 3class ViewController: UIPageViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 self.setViewControllers([getFlow()], direction: .forward, animated: true, completion: nil) 8 self.dataSource = self as UIPageViewControllerDataSource 9 } 10 11 func getFlow() -> FlowViewController { 12 return storyboard!.instantiateViewController(withIdentifier: "flow") as! FlowViewController 13 } 14 15 func getChat() -> ChatViewController { 16 return storyboard!.instantiateViewController(withIdentifier: "chat") as! ChatViewController 17 } 18 19 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 } 24} 25 26extension ViewController : UIPageViewControllerDataSource { 27 28 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 29 if viewController.isKind(of: ChatViewController.self) { 30 // 2 -> 1 31 return getFlow() 32 } else { 33 // 1 -> end of the road 34 return nil 35 } 36 } 37 38 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 39 if viewController.isKind(of: FlowViewController.self) { 40 // 1 -> 2 41 return getChat() 42 } else { 43 // 3 -> end of the road 44 return nil 45 } 46 } 47} 48
こちらをビルドし、segmentedcontrolの選択を変更するとpageviewcontrollerのクラスでエラーが発生します。
このエラーに従ってアンラップしても
Initializer for conditional binding must have Optional type, not 'ChatViewController'
とオプショナル型が必要だと出てきます。
また、画面遷移はここの部分で行っているのではないのでしょうか?
swift
1 func getFlow() -> FlowViewController { 2 return storyboard!.instantiateViewController(withIdentifier: "flow") as! FlowViewController 3 } 4 5 func getChat() -> ChatViewController { 6 return storyboard!.instantiateViewController(withIdentifier: "chat") as! ChatViewController 7 } 8
ご存知の方がいらっしゃいましたらよろしくお願いいたします。
追記
以下がprint(storyboard)の出力になります。
SegViewControllerとFlowViewController、およびChatViewControllerをセグエでつないだものです
実行して、segmentedcontrolを選択すると新しい画面としてChatViewControllerが出てきてしまいます。
SegViewControllerにContainerViewを乗せ、ChatVeiwControllerにSegue(embed)でつないだものです。
newmtさん
setViewControllersにgetChat()を追記したところ、立ち上がる前に落ちてしまいました。
こちらがその時のデバックエリアの画面です。
'The number of view controllers provided (2) doesn't match the number required (1) for the requested transition'
'提供されたビューコントローラの数(2)が、要求された遷移のために必要な数(1)と一致しません'
とありました。
回答2件
あなたの回答
tips
プレビュー