###前提・実現したいこと
xcode8 swift3 iPad
UICollectionViewを使用して、画像のスライドショーみたいなものを作っています。
各セルのサイズはフルスクリーンです。
各セルのレイアウトは違っていて、indexPathごとにカスタムセルでxibを使っています。
セルにボタンを配置してボタンをタップしたら、画像をモーダル表示したいです。
storyboardにViewControllerをUICollecitonViewを配置したものとは別に、新たに配置しています。
xibのボタンから新たに配置したViewControllerに遷移させたいです。
###発生している問題・エラーメッセージ
xibにボタンを配置しましたが、そこからのモーダル表示が上手くいきません。
UICollectionViewCellクラスからの遷移方法がわからず、色々と試していますが、上手くいっておりません。
selfのところでエラーとなっていますが、UICollectionViewクラスではないからと思いましたが、解決方法がわかっておりません。
###該当のソースコード
swift
1 2// カスタムセルクラス 3class CollectionViewCell2: CommonCollectionViewCell { 4 5 @IBOutlet weak var mainImage: UIImageView! 6 7 override func awakeFromNib() { 8 super.awakeFromNib() 9 // Initialization code 10 } 11 12 func setCell(imageName: String) { 13 self.mainImage.image = UIImage(named: imageName) 14 } 15 16}
swift
1// ViewControllerクラス 2class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 3 4 var screenHeight: CGFloat! 5 var screenWidth: CGFloat! 6 7 let screenNames = ["main01", "main02", "main03", "main09", "main02", "main03", "main09", "main02"] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 let statusBar = UIView(frame:CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 20.0)) 13 statusBar.backgroundColor = UIColor.white 14 view.addSubview(statusBar) 15 self.automaticallyAdjustsScrollViewInsets = false 16 17 self.collectionView!.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell1") 18 self.collectionView!.register(UINib(nibName: "CollectionViewCell2", bundle: nil), forCellWithReuseIdentifier: "cell2") 19 } 20 21 // 希望のセルサイズを返す 22 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 23 24 let screenSize: CGRect = UIScreen.main.bounds 25 screenWidth = screenSize.width 26 screenHeight = screenSize.height 27 let returnSize = CGSize(width: screenSize.width, height: screenSize.height) 28 29 return returnSize 30 } 31 32 //セルのアイテムのマージンを設定 33 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 34 return UIEdgeInsetsMake(20.0 , 0.0 , 0.0 , 0.0 ) //マージン(top , left , bottom , right) 35 } 36 37 //セル間のマージンを削除 38 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 39 return 0.0 40 } 41 42 //データの個数を返す 43 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 44 { 45 return screenNames.count 46 } 47 48 //データを返す 49 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 50 { 51 var cell: CommonCollectionViewCell! 52 if(indexPath.row == 0 || indexPath.row == 1) { 53 let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath as IndexPath) as! CollectionViewCell 54 cell1.setCell(imageName: screenNames[indexPath.row]) 55 cell = cell1 56 } 57 else { 58 let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath as IndexPath) as! CollectionViewCell2 59 cell2.setCell(imageName: screenNames[indexPath.row]) 60 cell = cell2 61 } 62 63 return cell 64 } 65 66 // ボタンタップで実行されるメソッド 67 @IBAction func modalView(_ sender: UIButton) { 68 print("tapButtonA") 69 } 70 71 override func didReceiveMemoryWarning() { 72 super.didReceiveMemoryWarning() 73 // Dispose of any resources that can be recreated. 74 } 75} 76 77
###コンソールでbtコマンドをたたいたときのログ
* thread #1: tid = 0x9380f, 0x0000000196863bdc libobjc.A.dylib`objc_msgSend + 28, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x110000) frame #0: 0x0000000196863bdc libobjc.A.dylib`objc_msgSend + 28 frame #1: 0x000000018988d398 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96 frame #2: 0x0000000189876474 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612 frame #3: 0x000000018988cd34 UIKit`-[UIControl touchesEnded:withEvent:] + 592 frame #4: 0x000000018988c9c0 UIKit`-[UIWindow _sendTouchesForEvent:] + 700 frame #5: 0x0000000189885efc UIKit`-[UIWindow sendEvent:] + 684 frame #6: 0x0000000189859120 UIKit`-[UIApplication sendEvent:] + 264 frame #7: 0x0000000189afa2b8 UIKit`_UIApplicationHandleEventFromQueueEvent + 15424 frame #8: 0x0000000189857634 UIKit`_UIApplicationHandleEventQueue + 1716 frame #9: 0x0000000184d98240 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 frame #10: 0x0000000184d974e4 CoreFoundation`__CFRunLoopDoSources0 + 264 frame #11: 0x0000000184d95594 CoreFoundation`__CFRunLoopRun + 712 frame #12: 0x0000000184cc12d4 CoreFoundation`CFRunLoopRunSpecific + 396 frame #13: 0x000000018e5df6fc GraphicsServices`GSEventRunModal + 168 frame #14: 0x00000001898bef40 UIKit`UIApplicationMain + 1488 * frame #15: 0x00000001000419f0 advicebook`main + 140 at AppDelegate.swift:12 frame #16: 0x0000000196efaa08 libdyld.dylib`start + 4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/13 07:46
2017/02/13 07:51
退会済みユーザー
2017/02/13 08:04
2017/02/13 08:08
退会済みユーザー
2017/02/13 09:38 編集
2017/02/13 08:44
退会済みユーザー
2017/02/13 09:01
2017/02/13 09:34
退会済みユーザー
2017/02/13 10:01
2017/02/14 00:41
退会済みユーザー
2017/02/14 07:22 編集
2017/02/14 07:44 編集
2017/02/14 07:47
退会済みユーザー
2017/02/14 08:09
2017/02/14 08:12 編集
退会済みユーザー
2017/02/14 09:14
2017/02/14 09:16
2017/02/14 09:18
退会済みユーザー
2017/02/14 09:38
2017/02/14 10:13
退会済みユーザー
2017/02/14 12:51
2017/02/15 00:28
退会済みユーザー
2017/02/15 07:12
2017/02/15 07:36
退会済みユーザー
2017/02/15 08:31
2017/02/15 08:37
退会済みユーザー
2017/02/15 08:44
2017/02/15 08:55 編集
退会済みユーザー
2017/02/15 09:23
2017/02/15 09:27 編集
退会済みユーザー
2017/02/15 10:10 編集
2017/02/15 10:10
退会済みユーザー
2017/02/15 13:48
2017/02/15 13:56
退会済みユーザー
2017/02/16 00:27
2017/02/16 00:30
2017/02/16 00:46
2017/02/16 01:31
退会済みユーザー
2017/02/16 01:50