如何从 UICollectionViewCell 中的按钮关闭 UICollectionViewController
How to dismiss UICollectionViewController from button in UICollectionViewCell
我正在制作一个入职屏幕,在最后一个屏幕上我有一个显示 "continue" 的按钮,应该关闭入职屏幕。入职屏幕是一个集合视图控制器,每个页面都有单元格。请不要犹豫,要求澄清我不知道还要添加什么。
谢谢,
编辑
所以我实现了用户 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)
}
我是否不需要在 class 中的任何地方调用该函数,只需将其留在主 class 之外即可。
编辑 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")
})
}
您可以使用委托,例如:
protocol YourCellDelegate: class {
func didCompleteOnboarding()
}
然后在您的手机中:
class CustomCell: UICollectionViewCell {
weak var delegate: YourCellDelegate?
// in your button action
func dismissAction() {
delegate.didCompleteOnboarding()
}
}
最后,在您的视图控制器中,在 cellForItem 函数中设置单元委托:
yourCell.delegate = self
并添加:
extension YourViewController: YourCellDelegate {
func didCompleteOnboarding() {
// dismiss here
}
}
我正在制作一个入职屏幕,在最后一个屏幕上我有一个显示 "continue" 的按钮,应该关闭入职屏幕。入职屏幕是一个集合视图控制器,每个页面都有单元格。请不要犹豫,要求澄清我不知道还要添加什么。
谢谢,
编辑 所以我实现了用户 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)
}
我是否不需要在 class 中的任何地方调用该函数,只需将其留在主 class 之外即可。
编辑 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")
})
}
您可以使用委托,例如:
protocol YourCellDelegate: class {
func didCompleteOnboarding()
}
然后在您的手机中:
class CustomCell: UICollectionViewCell {
weak var delegate: YourCellDelegate?
// in your button action
func dismissAction() {
delegate.didCompleteOnboarding()
}
}
最后,在您的视图控制器中,在 cellForItem 函数中设置单元委托:
yourCell.delegate = self
并添加:
extension YourViewController: YourCellDelegate {
func didCompleteOnboarding() {
// dismiss here
}
}