我正在尝试创建UIStoryboard,但是我的代码出现错误

时间:2018-12-06 16:24:43

标签: ios swift storyboard

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)
}    

错误是:

  

使用未解析的标识符“ storyboard”

2 个答案:

答案 0 :(得分:0)

看看这个:

    // 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的类名相同的ViewController分配情节提要ID。养成习惯,这将很有帮助,因为您无需记住设置的情节提要ID。因此,保持简单。

答案 1 :(得分:-1)

DetailViewController中检查Main.storyboard的属性,看看故事板ID是否设置正确。根据您的代码,它应该为vc1

enter image description here