質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

933閲覧

エラー「Unexpectedly found nil while implicitly unwrapping an Optional value」について

yotubarail

総合スコア23

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/06/14 08:20

UIPageViewControllerでのページ遷移の際に同時に動くタブを作成して、ビルドしようとしています。
イメージとしてはこちらのような感じです。

現状では
collectionView.datasource = self
のところでエラーが出てしまい、動作の確認ができていません。

解決法等お分かりになる方いらっしゃいましたら教えていただけますか。

Swift

1class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UICollectionViewDelegateFlowLayout { 2 3 4 let pageList = ["first", "second", "third"] 5 6 var collectionView:UICollectionView! 7 var page:Int = 0 8 let labelheight:CGFloat = 60 9 10 required init?(coder aDecoder: NSCoder) { 11 super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 12 } 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 dataSource = self 17 delegate = self 18 setViewControllers([storyboardId(strID: "first") as! UIViewController], direction: .forward, animated: true, completion: nil) 19 20 21 22 collectionView.dataSource = self // ビルドするとここでエラーが起こってしまいます 23 collectionView.delegate = self 24 25 // collectionViewレイアウト作成 26 CollectionViewLayoutSet() 27 28 // collectionViewその他設定 29 collectionView.showsHorizontalScrollIndicator = false 30 collectionView.showsVerticalScrollIndicator = false 31 collectionView.delaysContentTouches = false 32 33 view.addSubview(collectionView) 34 35 self.view.subviews 36 .filter{ $0.isKind(of: UIScrollView.self) } 37 .forEach{($0 as! UIScrollView).delegate = self } 38 } 39 40 41 func storyboardId(strID: String) -> AnyObject! { 42 return (storyboard?.instantiateViewController(withIdentifier: strID)) 43 } 44 45 // 1ページ進む 46 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 47 let index = pageList.firstIndex(of: viewController.restorationIdentifier!) 48 if index! < (pageList.count - 1) { 49 return (storyboardId(strID: pageList[index! + 1]) as! UIViewController) 50 } else { 51 return nil 52 } 53 } 54 55 56 // 1ページ戻る 57 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 58 let index = pageList.firstIndex(of: viewController.restorationIdentifier!) 59 if 0 < index! { 60 return (storyboardId(strID: pageList[index! - 1]) as! UIViewController) 61 } else { 62 return nil 63 } 64 } 65 66 // Page Controlを表示する 67 func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { 68 return pageList.count 69 } 70 func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { 71 return 0 72 } 73 74 75 76 77 func CollectionViewLayoutSet(){ 78 let flowLayout = UICollectionViewFlowLayout() 79 flowLayout.minimumInteritemSpacing = 0.0 80 flowLayout.minimumLineSpacing = 0.0 81 flowLayout.scrollDirection = .horizontal 82 flowLayout.itemSize = CGSize(width:viewframewidth / 3 , height:CGFloat(labelheight)) 83 let rec = CGRect(x: 0.0, y:statusBarHeight , width:viewframewidth , height: labelheight) 84 collectionView = UICollectionView(frame: rec, collectionViewLayout: flowLayout) 85 } 86} 87 88extension PageViewController: UICollectionViewDelegate, UICollectionViewDataSource { 89 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 90 return pageList.count 91 } 92 93 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 94 collectionView.register(cellType:tab.self) 95 let cell: tab = collectionView.dequeueReusableCell(with: tab.self, for: indexPath) 96 cell.Set(Text: pageList[indexPath.row]) 97 return cell 98 } 99 100 101} 102 103extension PageViewController { 104 func scrollViewDidScroll(_ scrollView: UIScrollView) { 105 if(scrollView.className == "UICollectionView"){ 106 return 107 } 108 109 let LeftRightJug = viewframewidth - scrollView.contentOffset.x 110 if(LeftRightJug == 0){ 111 return 112 } 113 let Move = scrollView.contentOffset.x / 3 114 let first:Int = LeftRightJug > 0 ? 1 : 0 115 let End:Int = LeftRightJug > 0 ? (pageList.count - 1) : (pageList.count - 2) 116 if(first < page && page < End) { 117 let pageWidth = CGFloat(page - 2) * viewframewidth 118 let TabPageWidth = pageWidth / 3 119 120 collectionView.contentOffset = CGPoint(x: Move + TabPageWidth, y:0) 121 } 122 } 123} 124 125

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こんにちは。

var collectionView:UICollectionView!

がnilなのでそのような実行時エラーがでます。

storyboardを使っているのでしたら@IBOutlet weak var collectionView:UICollectionView!(だっけ?)にして接続してください。

storyboardを使わないのであればUICollectionViewを生成してビューに追加するなりしてください。

投稿2019/06/14 08:28

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yotubarail

2019/06/14 08:40

質問なのですが、Storyboardを用いた場合UIPageViewController上にパーツを配置できないのですが、どのように配置すると良いのでしょうか
yotubarail

2019/06/14 08:47

ありがとうございます。 その方法で試してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問