首页 > 解决方案 > 我正在尝试创建 UIStoryboard 但我的代码中出现错误

问题描述

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    let vc = storyboard?.instantiateViewController(withIdentifier: "vc1")as? DetailViewController
    //vc?.image = img[indexPath.row]!
    //vc?.name = imageArray[indexPath.row] storyboard
    self.navigationController?.pushViewController(vc!, animated: true)
}    

错误是:

使用未解决的标识符“故事板”

标签: iosswiftstoryboard

解决方案


看看这个:

    // Case: When the next ViewController is existed in the same Storyboard.
    let controller = self.storyboard?.instantiateViewController(withIdentifier: "ControllerIdentifier") as! YourViewController
    // pass data here to the next controller.
    self.navigationController?.pushViewController(controller, animated: true)


    // Case: When the next ViewController is exists in other Storyboard.
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "ControllerIdentifier") as! YourViewController
    // pass data here to the next controller.
    self.navigationController?.pushViewController(controller, animated: true)

注意:当您使用此行'withIdentifier:“vc1”'时,我建议您始终为 ViewController 分配 Storyboard ID,与 ViewController 的类名相同。让它成为一种习惯,这会很有帮助,因为您不需要记住您设置的 Storyboard Id。所以保持简单。


推荐阅读