我是swift的新手,我按照這個視頻做了一個集合視圖,效果很好。但從一個單元格點擊到另一個單元格的時候,卻無法作業。
https://www.youtube.com/watch?v=TQOhsyWUhwg
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
print("Cell (indexPath.row 1)/span> clicked")
在這里,它正在列印被選中的單元格。我只需要在單元格被點擊時打開另一個視圖。誰能幫幫我。
uj5u.com熱心網友回復:
你只需要創建另一個視圖控制器的物件并推送它。 如:
let storyBoard : UIStoryboard = UIStoryboard( name: "Main", bundle:nil)
let nextViewController = storyBoard. instantiateViewController(withIdentifier: "your_view_identifier") 為! Your_ViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
uj5u.com熱心網友回復:
讓我們猜想一下,你要導航的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"/span>)
self.navigationController?.pushViewController(secondViewController, animated: true)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/307965.html
標籤:
