gpt4 book ai didi

swift - 如何从 UICollectionViewCell 返回到之前的 ViewController?

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:05 26 4
gpt4 key购买 nike

Main.storyboard 中有两个页面( View Controller ):FirstViewControllerSecondViewController。我在 FirstViewController 中添加了一个按钮。这是按钮点击范围:

    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainPage") as! SecondViewController
controller.modalPresentationStyle = .fullScreen
show(controller, sender: nil)

我已将 UICollectionView 添加到 SecondViewController 中。 UICollectionViewCell 也被添加(使用 .xib 文件)我已经在 .xib 文件中添加了后退按钮。我想为后退按钮操作编写下面的代码(请关注 func btnBack)

import UIKit

class ViewCell: UICollectionViewCell {

@IBOutlet weak var lblOutlet: UILabel!
@IBOutlet weak var contentViewOutlet: UIView!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

@IBAction func btnBack(_ sender: UIButton) {
present(controller, animated: true, completion: nil)
}
}

但是如您所知,ViewCell 没有成员 dismiss。我正在尝试做的是使用 UICollectionViewCell 中的后退按钮返回到 FirstViewController。是的,应用程序用户会在每个 ui 集合记录中看到此后退按钮。

最佳答案

也许您可以使用协议(protocol)和委托(delegate)模式从第二个 View Controller 弹出。您可以在 CollectionViewCell 文件中添加一个协议(protocol),例如

protocol popBackDelegate { 
func popBack()
}

然后将其注入(inject)您的 CollectionViewCell

var popBackDelegate: popBackDelegate!

之后你可以在你的 btnBack Action 中调用它

popBackDelegate.popBack()

现在你必须将它绑定(bind)到你的第二个 View Controller 的 Collection View 的 cellforitem 事件中

cell.popBackDelegate = self

最后,您可以在您的 View Controller 中遵循此协议(protocol)功能并在其中弹出。

希望我能帮到你。

关于swift - 如何从 UICollectionViewCell 返回到之前的 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58110025/

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