gpt4 book ai didi

ios - 如何从 UICollectionViewCell 中的按钮关闭 UICollectionViewController

转载 作者:行者123 更新时间:2023-11-28 07:35:34 25 4
gpt4 key购买 nike

我正在制作一个入职屏幕,在最后一个屏幕上我有一个按钮,上面写着“继续”并且应该关闭入职屏幕。入职屏幕是一个 Collection View Controller ,每个页面都有单元格。请不要犹豫,要求澄清我不知道还要添加什么。

谢谢,

编辑所以我实现了用户 Francesco Deliro 的回答,第一个问题是我不小心将“delegate = self”添加到 viewDidLoad() 中。我修复了这个问题,但 viewController 仍然没有关闭。我的代码在项目的 viewController 单元格中如下所示:

    let loginCell = LoginCell()
loginCell.delegate = self

这是扩展

extension TutorialViewController: LoginCellDelegate {
func didCompleteOnboarding() {
print("I should dimiss")
self.dismiss(animated: true, completion: nil)
}

我是否不需要在类中的任何地方调用该函数,只需将其留在主类之外即可。

编辑 2以下是我如何将按钮操作与原始按钮相关联

    @objc func continueTapped() {
...
continueButton.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .allowUserInteraction, animations: { [weak self] in
self?.continueButton.transform = .identity
let transition = CATransition()
transition.duration = 0.5
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
self?.window!.layer.add(transition, forKey: kCATransition)
self?.delegate?.didCompleteOnboarding()
}, completion: { (success) in
token = true
defaults.set(token, forKey: "DidSee")
})
}

最佳答案

你可以使用委托(delegate),例如:

protocol YourCellDelegate: class {
func didCompleteOnboarding()
}

然后在你的单元格中:

class CustomCell: UICollectionViewCell {
weak var delegate: YourCellDelegate?

// in your button action
func dismissAction() {
delegate.didCompleteOnboarding()
}

}

最后,在您的 View Controller 中,在 cellForItem 函数中设置单元委托(delegate):

yourCell.delegate = self

并添加:

extension YourViewController: YourCellDelegate {
func didCompleteOnboarding() {
// dismiss here
}
}

关于ios - 如何从 UICollectionViewCell 中的按钮关闭 UICollectionViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53252815/

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