从没有故事板的 collectionView 中显示 viewController
show viewController from collectionView without storyboards
我在以编程方式(没有故事板)从 collectionView 显示 viewController 时遇到了一些问题。我认为这很容易,但由于我是 swift 的新手,所以我很难理解这一点。我相信 collectionViewCells 默认情况下不是委托,所以我尝试在 collectionView class 中实现 didSelectItemAt 但仍然没有运气。点击单元格后,我收到了一份打印声明,只是没有任何显示转场。请参阅下面的 collectionViewCell 代码,在此先感谢!
// collectionViewCell class
class PlanningCell: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()
//collectionViewCell variables
var plannedPlaces = [CurrentPlanner]()
let cellId = "cellId"
var basePlanningCell: BasePlanningCell!
override func setupViews() {
super.setupViews()
addSubview(collectionView)
collectionView.register(BasePlanningCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return plannedPlaces.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BasePlanningCell
cell.currentPlanner = plannedPlaces[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("did tap")
let plannersDetailVC = PlanningPlaceDetailsVC()
plannersDetailVC.navigationTitle = plannedPlaces[(indexPath.row)].planningPlace
// below I am receiving the use of unresolved identifier "show" error
show(plannersDetailVC, sender: self)
}
}
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "VCIdentifier") as! PlanningPlaceDetailsVC
present(vc, animated: true, completion: nil)
不要忘记设置 PlanningPlaceDetailsVC Identifier
已针对代码设置的 VC 更新:
let vc : UIViewController = PlanningPlaceDetailsVC()
self.presentViewController(vc, animated: true, completion: nil)
通常情况下,当你想呈现另一个视图控制器时,你需要调用这个函数
self.present(yourViewController, animated: true, completion: nil)
但是,您不能在打印的地方执行此操作,因为函数 present
仅在视图控制器中可用。所以现在问题就变成了,如何将您的单元格点击定向到父视图控制器并传递一个字符串。你有两个选择,你可以创建一个像 this, or do a 这样的委托,第二个更容易。
当您将单元格点击事件传递给您的父视图控制器时,您可以开始调用上面提到的方法以导航到另一个视图控制器
我在以编程方式(没有故事板)从 collectionView 显示 viewController 时遇到了一些问题。我认为这很容易,但由于我是 swift 的新手,所以我很难理解这一点。我相信 collectionViewCells 默认情况下不是委托,所以我尝试在 collectionView class 中实现 didSelectItemAt 但仍然没有运气。点击单元格后,我收到了一份打印声明,只是没有任何显示转场。请参阅下面的 collectionViewCell 代码,在此先感谢!
// collectionViewCell class
class PlanningCell: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()
//collectionViewCell variables
var plannedPlaces = [CurrentPlanner]()
let cellId = "cellId"
var basePlanningCell: BasePlanningCell!
override func setupViews() {
super.setupViews()
addSubview(collectionView)
collectionView.register(BasePlanningCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return plannedPlaces.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BasePlanningCell
cell.currentPlanner = plannedPlaces[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("did tap")
let plannersDetailVC = PlanningPlaceDetailsVC()
plannersDetailVC.navigationTitle = plannedPlaces[(indexPath.row)].planningPlace
// below I am receiving the use of unresolved identifier "show" error
show(plannersDetailVC, sender: self)
}
}
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "VCIdentifier") as! PlanningPlaceDetailsVC
present(vc, animated: true, completion: nil)
不要忘记设置 PlanningPlaceDetailsVC Identifier
已针对代码设置的 VC 更新:
let vc : UIViewController = PlanningPlaceDetailsVC()
self.presentViewController(vc, animated: true, completion: nil)
通常情况下,当你想呈现另一个视图控制器时,你需要调用这个函数
self.present(yourViewController, animated: true, completion: nil)
但是,您不能在打印的地方执行此操作,因为函数 present
仅在视图控制器中可用。所以现在问题就变成了,如何将您的单元格点击定向到父视图控制器并传递一个字符串。你有两个选择,你可以创建一个像 this, or do a
当您将单元格点击事件传递给您的父视图控制器时,您可以开始调用上面提到的方法以导航到另一个视图控制器