Swift打开新的UIViewController

时间:2017-04-02 22:52:02

标签: ios uiviewcontroller swift3

当我点击UIViewController内的项目时,我正在尝试打开新的UICollectionView,但使用此代码我的应用程序崩溃,Xcode和模拟器重新启动,所以我甚至看不到它在哪里问题

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

        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

试试这个:

  1. 让您不要忘记在您的segue中声明 postController 标识符
  2. enter image description here

    1. 您只需以模态方式呈现:
    2. 覆盖func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath){

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