gpt4 book ai didi

ios - 当前操作后将 Collection View 的委托(delegate)设置为 self

转载 作者:行者123 更新时间:2023-11-29 05:26:46 28 4
gpt4 key购买 nike

Main.storyboard中有2个页面:FirstViewControllerSecondViewController

FirstViewController 中有一个按钮。这是按钮点击范围:

let controller = SecondViewController()
present(controller, animated: true, completion: nil)

并且在SecondViewController中有一个 Collection View 组件。

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var collectionViewOutlet: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()

collectionViewOutlet.delegate = self
collectionViewOutlet.dataSource = self


let xib = UINib(nibName: "ViewCell", bundle: nil)
self.collectionViewOutlet.register(xib, forCellWithReuseIdentifier: "ViewCellID")

let v0 = self.storyboard?.instantiateViewController(withIdentifier: "subView") as! SubViewController
temp_containerViewController = v0
}
}

extension MainPageViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.myPageList.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionViewOutlet.dequeueReusableCell(withReuseIdentifier: "ViewCellID", for: indexPath) as! ViewCell
let viewController = self.temp_containerViewController
cell.contentViewOutlet = viewController?.view
cell.contentViewOutlet.backgroundColor = .brown
cell.lblOutlet.text = self.myPageList[indexPath.row]

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionViewOutlet.frame.size
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let index = self.collectionViewOutlet.contentOffset.x / self.collectionViewOutlet.frame.size.width
self.pageControlOutlet.currentPage = Int(index)
self.pageControlOutlet.numberOfPages = myPageList.count
}
}

但是下面的代码给出了错误:

collectionViewOutlet.delegate = self

如何解决这个问题?我已经为 Collection View 组件创建了一个导出,如上所示。错误语句如下:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

最佳答案

您需要加载此行

let controller = SecondViewController()

来自 Storyboard(为其指定标识符第二)

let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Second") as! SecondViewController

因为您有没有 XIB 的网点

关于ios - 当前操作后将 Collection View 的委托(delegate)设置为 self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58094510/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com