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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

Q&A

解決済

1回答

2007閲覧

UIViewControllerからUICollectionViewControllerへの遷移方法を教えてください!

snakelegs

総合スコア26

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

0グッド

0クリップ

投稿2019/11/13 12:49

編集2019/11/14 01:46

UIViewControllerからUICollectionViewControllerへの遷移方法を教えてください

MyFirstViewControllerからボタンを押してMySecondCollectionViewControllerに遷移方法を教えて欲しいです。

MyFirstViewController

swift

1import UIKit 2 3class MyFirstViewController: UIViewController { 4 let mybutton:UIButton = UIButton() 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 //ボタン 8 let mybuttonrect:CGRect = CGRect(x:100, y: 100, width: 100, height: 100) 9 mybutton.frame = mybuttonrect; 10 mybutton.backgroundColor = UIColor.black 11 mybutton.addTarget(self, action: #selector(mybuttonEvent(_:count:)), for: UIControl.Event.touchUpInside) 12 self.view.addSubview(mybutton) 13 // Do any additional setup after loading the view. 14 } 15 @objc func mybuttonEvent(_ sender: UIButton,count:Int) { 16 let vc = MySecondCollectionViewController() 17 vc.modalPresentationStyle = .fullScreen 18 self.present(vc,animated: false,completion: nil) 19 } 20} 21

MySecondCollectionViewController

swift

1import UIKit 2import TRMosaicLayout 3 4private let reuseIdentifier = "cell" 5 6class MySecondCollectionViewController: UICollectionViewController{ 7 let imagearray = ["image1", "image2", "image3", "image4", "image5", "image6","image7.png","image8.png"] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 let mosaicLayout = TRMosaicLayout() 12 self.collectionView?.collectionViewLayout = mosaicLayout 13 mosaicLayout.delegate = self 14 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 15 } 16 17 18 override func numberOfSections(in collectionView: UICollectionView) -> Int { 19 return 1 20 } 21 22 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 23 return imagearray.count 24 } 25 26 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 27 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 28 let imageView = UIImageView(image: UIImage(named: imagearray[indexPath.row])) 29 imageView.frame = cell.frame 30 cell.backgroundView = imageView 31 // Configure the cell 32 return cell 33 } 34} 35extension MySecondCollectionViewController: TRMosaicLayoutDelegate { 36 func collectionView(_ collectionView: UICollectionView, mosaicCellSizeTypeAtIndexPath indexPath: IndexPath) -> TRMosaicCellType { 37 return indexPath.item % 3 == 0 ? TRMosaicCellType.big : TRMosaicCellType.small 38 } 39 40 func collectionView(_ collectionView:UICollectionView, layout collectionViewLayout: TRMosaicLayout, insetAtSection:Int) -> UIEdgeInsets { 41 return UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3) 42 } 43 44 func heightForSmallMosaicCell() -> CGFloat { 45 return 150 46 } 47} 48

エラー内容

で実行し、画面遷移するボタンを押したところエラーが発生しました。以下にエラー内容を示します。

error

1Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 2*** First throw call stack: 3( 4 0 CoreFoundation 0x00007fff23baa1ee __exceptionPreprocess + 350 5 1 libobjc.A.dylib 0x00007fff50864b20 objc_exception_throw + 48 6 2 CoreFoundation 0x00007fff23baa02c +[NSException raise:format:] + 188 7 3 UIKitCore 0x00007fff46d5793d -[UICollectionView initWithFrame:collectionViewLayout:] + 205 8 4 UIKitCore 0x00007fff46e49401 -[UICollectionViewController _newCollectionViewWithFrame:collectionViewLayout:] + 75 9 5 UIKitCore 0x00007fff46e48688 -[UICollectionViewController loadView] + 642 10 6 UIKitCore 0x00007fff46f0895f -[UIViewController loadViewIfRequired] + 172 11 7 UIKitCore 0x00007fff46f0910c -[UIViewController view] + 27 12 8 UIKitCore 0x00007fff46e22176 -[_UIFullscreenPresentationController _setPresentedViewController:] + 84 13 9 UIKitCore 0x00007fff46e152f7 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 184 14 10 UIKitCore 0x00007fff46f1a2fe -[UIViewController _presentViewController:withAnimationController:completion:] + 3553 15 11 UIKitCore 0x00007fff46f1cf97 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke_2 + 70 16 12 UIKitCore 0x00007fff47a44c3e +[UIView(Animation) performWithoutAnimation:] + 84 17 13 UIKitCore 0x00007fff46f1ceec __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 211 18 14 UIKitCore 0x00007fff46f1d393 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511 19 15 UIKitCore 0x00007fff46f1cdd9 -[UIViewController _presentViewController:animated:completion:] + 187 20 16 UIKitCore 0x00007fff46f1d040 -[UIViewController presentViewController:animated:completion:] + 150 21 17 ****** 0x0000000104d82188 $s6******21MyFirstViewControllerC13mybuttonEvent_5countySo8UIButtonC_SitF + 168 22 18 ****** 0x0000000104d821ec $s6******21MyFirstViewControllerC13mybuttonEvent_5countySo8UIButtonC_SitFTo + 76 23 19 UIKitCore 0x00007fff4757a082 -[UIApplication sendAction:to:from:forEvent:] + 83 24 20 UIKitCore 0x00007fff46f608e5 -[UIControl sendAction:to:forEvent:] + 223 25 21 UIKitCore 0x00007fff46f60c2f -[UIControl _sendActionsForEvents:withEvent:] + 398 26 22 UIKitCore 0x00007fff46f5fb8e -[UIControl touchesEnded:withEvent:] + 481 27 23 UIKitCore 0x00007fff475b4a31 -[UIWindow _sendTouchesForEvent:] + 2604 28 24 UIKitCore 0x00007fff475b6338 -[UIWindow sendEvent:] + 4596 29 25 UIKitCore 0x00007fff47591693 -[UIApplication sendEvent:] + 356 30 26 UIKitCore 0x00007fff47611e5a __dispatchPreprocessedEventFromEventQueue + 6847 31 27 UIKitCore 0x00007fff47614920 __handleEventQueueInternal + 5980 32 28 CoreFoundation 0x00007fff23b0d271 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 33 29 CoreFoundation 0x00007fff23b0d19c __CFRunLoopDoSource0 + 76 34 30 CoreFoundation 0x00007fff23b0c974 __CFRunLoopDoSources0 + 180 35 31 CoreFoundation 0x00007fff23b0767f __CFRunLoopRun + 1263 36 32 CoreFoundation 0x00007fff23b06e66 CFRunLoopRunSpecific + 438 37 33 GraphicsServices 0x00007fff38346bb0 GSEventRunModal + 65 38 34 UIKitCore 0x00007fff47578dd0 UIApplicationMain + 1621 39 35 ****** 0x0000000104d7fd9b main + 75 40 36 libdyld.dylib 0x00007fff516ecd29 start + 1 41) 42libc++abi.dylib: terminating with uncaught exception of type NSException 43(lldb)

ご教示お願いします。

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

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

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

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

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

thyda.eiqau

2019/11/13 16:50

「reason: 'UICollectionView must be initialized with a non-nil layout parameter'」と記載されていますが、こちらに対してどのようなアクションを取られましたか
snakelegs

2019/11/13 17:16

エラー内容をネットで調べ、MyFirsViewtControllerの画面遷移のコードに let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width:view.frame.width, height:view.frame.height) vc.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout) を追加して見ましたが、 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' とエラーが出ました。
takabosoft

2019/11/14 01:44

SwiftUIタグは関係ないので外してください。
guest

回答1

0

ベストアンサー

遷移方法ではなくて、UICollectionViewControllerの使い方が判らないだけですよね?
また、Storyboardを使わないスタイルだと思いますが、であれば、

swift

1class MySecondCollectionViewController: UICollectionViewController{ 2 init() { 3 let mosaicLayout = TRMosaicLayout() 4 super.init(collectionViewLayout: mosaicLayout) 5 } 6 7 required init?(coder aDecoder: NSCoder) { 8 fatalError("init(coder:) has not been implemented") 9 } 10}

などとイニシャライザでレイアウトを指定してみてはいかがでしょうか。

投稿2019/11/14 00:53

takabosoft

総合スコア8356

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

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

snakelegs

2019/11/14 01:52

ありがとうございます!swiftをはじめたばかりで、UICollectionViewControllerの使い方の問題のようでしたすいません。実行して見たところエラーが出なくなりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問