2017-04-02 71 views
-2

我试图打开新的UIViewController当我点击UICollectionView里面的一个项目,但是使用这个代码我的应用程序崩溃,Xcode和模拟器重新启动,所以我什至不能看到问题出在哪里。快速打开新的UIViewController

import UIKit 

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: "cellId") 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CustomCell 

     cell.imageViewGame.image = UIImage(named: imageArray[indexPath.row]) 

     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: view.frame.width/2, height: view.frame.width/2) 
    } 

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     performSegue(withIdentifier: "postController", sender: title[indexPath.row]) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "postController" { 
      let postController = segue.destination as! PostController 

      postController.title = sender as! String 

     } 
    } 
} 

有没有人知道从这段代码为什么我的应用程序崩溃?如果重要,我不使用故事板。

+0

你也必须写,你得到错误信息。我们不是预兆。 –

+0

1.调试日志说什么? 2. postController标识符是否存在? – carlos21

+0

我看不到错误消息,我的Xcode每次崩溃。 – user3593157

回答

0

试试这个:

  1. 请你不要忘记你的SEGUE声明PostController中标识

enter image description here

  • 你只可以用模态表示:
  • 覆盖FUNC的CollectionView(_的CollectionView:UICollectionView,didSelectItemAt indexPath:IndexPath){

     let postController = PostController() 
         postController.title = title[indexPath.row] 
         present(postController, animated: true, completion: nil) 
        } 
    
    +0

    我根本不使用故事板。我曾经在故事板上遇到过一些问题,所以我决定像一些专业人士所建议的那样放弃它。现在我试图找出如何在没有故事板的情况下做到这一点。 – user3593157

    +0

    如果你正在使用storyboard,你的segue的定义在哪里? – carlos21

    +0

    我没有。我认为这不是必需的,它只是为了准备func,所以它可以确定它应该执行哪个seque。 – user3593157