我是swift的新手,我跟随这个视频做了一个集合视图,它工作得很好。但是在从一个单元格到另一个单元格单击时,单击并不起作用。
https://www.youtube.com/watch?v=TQOhsyWUhwg
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
print("Cell \(indexPath.row + 1) clicked")
}
在这里,它打印选定的单元格。当单元格被单击时,我只需要打开另一个视图。有人能帮我吗。
发布于 2021-09-10 20:55:03
你只需要创建另一个视图控制器的对象并推送它。像这样:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "your_view_identifier") as! Your_ViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
发布于 2021-09-10 22:25:31
让我们猜猜您想要导航的viewController是SecondViewController
。故事板的名字是Main
。
导航ViewController的步骤
您需要创建该ViewController
推送该viewController
class SecondViewController: UIViewController {
}
var secondViewController: SecondViewController {
let st = UIStoryboard(name: "Main", bundle: nil)
let vc = st.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
return vc
}
现在,如果你想从单元格点击导航视图控制器。只需将视图控制器推送到导航堆栈中。
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
print("Cell \(indexPath.row + 1) clicked")
self.navigationController?.pushViewController(secondViewController, animated: true)
}
https://stackoverflow.com/questions/69139761
复制