ios - 我正在尝试创建 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)
}
错误是:
使用未解决的标识符“故事板”
解决方案
看看这个:
// 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。所以保持简单。
推荐阅读
- javascript - ChartsAmchartsBookingStatisticsLYC 不是函数
- r - 如何使用条件语句为条形图着色
- ios - 如何使用 Alamofire(或 NSUrlSession)使调试日志静音?
- python-3.x - 在循环中附加不等行的numpy数组
- javascript - 在打字稿中排序对象数组时出错,没有正确的顺序(使用 .slice() 工作)
- php - 尝试删除时 Laravel 模型未加载数据
- powershell - PowerShell 中 Win32 可执行输出的字母之间的单个空格
- python - 如何在python中使用xpath在h1标签内获取末尾有空格的文本时消除“\ t”?
- python - 自动生成的小部件上的 PyQt 信号
- javascript - 从数据库中删除图像