如何从笔尖单元中呈现视图

发布于 2025-01-28 06:02:56 字数 167 浏览 2 评论 0原文

我有一个收集视图,并在Uinib上注册单元,但似乎无法在Uinib单元格内显示一个ViewController。

我想要一个ViewController,但是Nib单元格中的代码'self.present(.....)'不起作用。

如何从CEOLLECTIONVIEW NIB单元中呈现视图?

I have a collection view, and register cell with UINib, but it seems can't present a viewcontroller inside the UINib cell.

I want present a viewController, but the code inside the nib cell 'self.present(.....)' is not working.

How present a view from a ceollectionView nib cell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2025-02-04 06:02:56

存在是视图控制器上的一种方法,而不是视图。如果您想提供另一个视图控制器,则需要让当前视图控制器知道它。在Uikit中,这通常是通过代表模式完成的。

您需要创建自己的委托协议并在视图控制器中实现。

protocol MyDelegate: AnyObject {
    func presentSomething(from cell: MyCell)
}
extension MyViewController: MyDelegate {
    func presentSomething(from cell: MyCell) {
        present(....
    }
}

并将委托属性添加到单元格:

class MyCell: UICollectionViewCell {
    weak var delegate: MyDelegate?
}

集合视图数据源通常是托管收集视图的视图控制器。当您配置收集视图单元格时,您可以在这一点上设置单元格的委托:

... your existing cell configuration ...
cell.delegate = self
return cell

假设您想在父视图控制器上介绍某些内容,以响应未选择的小区的特定操作。

如果您选择了单元格,则只需使用didSelect集合查看视图委托方法即可。

如果您想添加将生活在收集视图单元格中的子视图控制器,那么您可能想重新考虑的方法略有不同(由于细胞重复使用而变得更加危险)。

present is a method on a view controller, not a view. If you want to present another view controller, you'll need to let the current view controller know about it. In UIKit this is often done via the delegate pattern.

You will need to create your own delegate protocol and implement it in the view controller.

protocol MyDelegate: AnyObject {
    func presentSomething(from cell: MyCell)
}
extension MyViewController: MyDelegate {
    func presentSomething(from cell: MyCell) {
        present(....
    }
}

And add a delegate property to the cell:

class MyCell: UICollectionViewCell {
    weak var delegate: MyDelegate?
}

Collection view data sources are commonly the view controller that hosts the collection view. When you are configuring the collection view cell, you can set the delegate of the cell at this point:

... your existing cell configuration ...
cell.delegate = self
return cell

This assumes you want to present something on the parent view controller in response to a particular action on the cell that is not selection.

If you're selecting the cell, then just use the didSelect collection view delegate method.

If you want to add a child view controller that is going to live inside the collection view cell, then that's a slightly different (and more fraught with danger, because of cell reuse) approach which you may want to rethink.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文