简体   繁体   中英

using didSelectItem for programmed UICollectionViewCell

I have programmed a UICollectionView and when the cell is select I would like for it to go to another view cotroller. I am getting the error message of

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "twoVC") as? twoVC

        self.navigationController?.pushViewController(vc!, animated: true)
        //Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value     
    }

}

class twoVC : UIViewController {
    @IBOutlet var label : UILabel!

    @IBOutlet var photo : UIImageView!

}

You are force unwrapping vc so chances are that it is nil which means that likely "twoVC" is not the identifier for that view controller in the storyboard. Double check that in your storyboard:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM