簡體   English   中英

我在CollectionViewCell內部有一個UIButton,它又在TableViewCell內部。如何單擊UIButton導航到下一個視圖控制器?

[英]I have a UIButton inside a CollectionViewCell which is again inside the TableViewCell.How do i navigate to next view contoller by clicking UIButton?

我在UITableViewCell里面的UICollectionViewCell內部有UIButton ,我需要在點擊該UIButton導航另一個UIViewController 但是UINavigationControllerUICollectionViewCell內部UICollectionViewCell 有人可以幫忙嗎?

我在代碼中找不到任何NavigationController。

import UIKit

class TrendingProductTVCell: UITableViewCell {

    @IBOutlet weak var trendingProductCV: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()
        trendingProductCV.delegate = self
        trendingProductCV.dataSource = self
    }
}


extension TrendingProductTVCell: UICollectionViewDataSource, UICollectionViewDelegate {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = trendingProductCV.dequeueReusableCell(withReuseIdentifier: "TrendingProductsCVCell", for: indexPath) as? TrendingProductsCVCell
        cell?.trendingAddToCartBtn.addTarget(self, action: #selector(addToCartBtnTapped), for: .touchUpInside)
        return cell!
    }

    @objc
    func addToCartBtnTapped(sender: UIButton){
       let productDetail = trendingProductsDataArray[sender.tag]
    }
}

您可以使用委托協議。

protocol TrendingProductTVCellDelegate {
    func addToCart(product: Product)
}

class TrendingProductTVCell: UITableViewCell {
    var delegate: TrendingProductTVCellDelegate?
}

extension TrendingProductTVCell: UICollectionViewDataSource, UICollectionViewDelegate {
    @objc func addToCartBtnTapped(sender: UIButton){
       let productDetail = trendingProductsDataArray[sender.tag]
       delegate?.addToCart(product: productDetail)
    }
}

UINavigationController只能通過ViewController訪問。 因此,您的TrendingProductViewController必須偵聽協議,然后才能訪問navigationController。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = ... // your TrendingProductTVCell
    cell.delegate = self // <- important
    ...
}

extension TrendingProductViewController: TrendingProductTVCellDelegate {
    func addToCart(product: Product) {
        navigationController.push() // your rest code here....
    }
}

您可以做兩件事之一

1.使用NotificationCenter發布通知 ,在userInfo中發送productDetail ,並在包含tableViewViewController中監聽通知
2.或者,您可以簡單地使用要在選擇產品時觸發的方法創建一個協議 ,在TableViewCell中添加一個委托屬性,在創建單元時發送控制器的引用(在cellForRowAt:中 ),然后調用選擇產品時在該委托上的方法中。

盡管我更喜歡使用第二種方法,但是通過兩種方式,您將能夠處理新視圖控制器的推送。

使用委托:通過委托,您可以向調用方提供更多信息,在這種情況下,它更加靈活。 最佳解決方案使用委托。

您可以使用此擴展名為任何視圖獲取parentViewController:

extension UIView {
  var parentViewController: UIViewController? {
    var parentResponder: UIResponder? = self
    while parentResponder != nil {
        parentResponder = parentResponder!.nextResponder()
        if parentResponder is UIViewController {
            return parentResponder as! UIViewController!
        }
    }
    return nil
  }
}

然后,您將可以訪問UITableViewCellUICollectionViewCell 獲取此parentViewController的navController,然后從UICollectionViewCell內部推送到下一個。

暫無
暫無

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

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