簡體   English   中英

UIVIewController 彈出時未取消初始化

[英]UIVIewController Not Getting Deinitialized When Popping

我正在一個應用程序中構建一個設置屏幕,其中有一個單元格列表。 如果用戶點擊一個單元格,它會將另一個 controller 推到堆棧上。 但是,我在我的應用程序的幾個地方都有這個流程。

因此,我決定重用一個通用的 controller 並用部分初始化它(取決於哪個單元被點擊)

但是,當彈出 UIViewController 時,它不會被取消初始化

查看 CONTROLLER 代碼

// Class    
class ProfileController: UIViewController {
    
private let authService: AuthSerivce
private let sections: [FormSectionComponent]
init(authService: AuthSerivce,
     sections: [FormSectionComponent]) {
    self.authService = authService
    self.sections = sections
    super.init(nibName: nil, bundle: nil)
    }
}
// Cell Delegate
extension ProfileController: NavigateCellDelegate {
    
func navigate(cell: NavigateCell) {
    guard let sections = cell.item?.components else { return }
    let controller = ProfileController(authService: authService, sections: sections)
    self.navigationController?.pushViewController(controller, animated: true)
    }
}

單元格代碼

protocol NavigateCellDelegate {
    func navigate(cell: NavigateCell)
}
class NavigateCell: UICollectionViewCell {
    
    var item: NavigateComponent?
    var delegate: NavigateCellDelegate?
    
    lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func bind(_ item: FormItemComponent) {

        guard let item = item as? NavigateComponent else { return }
        self.item = item
        setUpView(item: item)
        addTapGestureRecogniser()
    }
    
    func addTapGestureRecogniser() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture))
        self.addGestureRecognizer(tapGesture)
        self.isUserInteractionEnabled = true
    }
    
    @objc func tapGesture() {
        delegate?.navigate(cell: self)
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        titleLabel.text = ""
    }
}

extension NavigateCell {
    
    func setUpView(item: NavigateComponent) {
        
        titleLabel.text = item.title
        addSubview(titleLabel)
        NSLayoutConstraint.activate([
            titleLabel.topAnchor.constraint(equalTo: topAnchor),
            titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
            titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
            titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
        ])
    }
} // END

更新了單元格中的弱代表

protocol NavigateCellDelegate: AnyObject {
    func navigate(cell: NavigateCell)
}

class NavigateCell: UICollectionViewCell {


weak var item: NavigateComponent?
weak var delegate: NavigateCellDelegate?

想通了-問題出在我的 DiffableDataSource 上,而不是聲明 [weak self]

return UICollectionViewDiffableDataSource(collectionView: profileView.collectionView) { [weak self] collectionView, indexPath, item in

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM