簡體   English   中英

從UICollectionViewCell xib到UIViewController的Segue

[英]Segue from UICollectionViewCell xib to UIViewController

我有UICollectionViewCell(啟用了分頁,寬度和高度等於視圖),它在單元格內有另一個UICollectionView(第二級,以編程方式添加)。 第二級UICollectionView具有使用xib(CustomMenuViewController)的UICollectionViewCell。 在這個xib文件中,我有一個按鈕,通過按下該按鈕,我想轉到另一個UIViewController。

class Cellnew: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{

    let cellId = "mycell";

    override init(frame: CGRect) {
        super.init(frame: frame);

        setupViews();
        self.collectionView.register(UINib(nibName: "subMenuCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "mycell")
           }

    lazy var menuBar : MenuBar = {
        let mb = MenuBar()
        return mb

    }()

    func setupViews(){
        addSubview(collectionView);

        collectionView.delegate = self;
        collectionView.dataSource = self;

        collectionView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true;
        collectionView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true;
        collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true;
        collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true;
        collectionView.heightAnchor.constraint(equalToConstant: 200).isActive = true


    }

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.minimumLineSpacing = 30
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical; //set scroll direction to horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout);
        cv.backgroundColor = UIColor.hexStringToUIColor(hex: "F9F9F9")
        cv.translatesAutoresizingMaskIntoConstraints = false;
        cv.contentInset = UIEdgeInsetsMake(50, 0, 150, 0)

        return cv;
    }();


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell : subMenuCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! subMenuCollectionViewCell

        cell.mImagePress.addTarget(self,action:#selector(self.toCustommenu), for: .touchUpInside)
        return cell;
        }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(20, 10, 20, 10);
                }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5;
                }



  @objc func toCustommenu(){

      //What can i do here


                }




    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.reloadData()
        print("menutapped")
        let myDict: [String: Any] = ["CellNo": indexPath.row]
        NotificationCenter.default.post(name: .refresh, object: myDict)


    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:( self.frame.width/2)-10-10, height: self.frame.height/3);
    }
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我假設您有一個初始UIViewController ,該UIViewController包含一個故事板中的最外面的集合視圖。 我還假設這是應該過渡到由第二個UIViewController控制的另一個視圖的視圖。

由於您是在集合視圖內進行編程工作,因此最好的選擇是從初始視圖控制器到輔助視圖控制器創建一個序列,並按名稱觸發它。

去做這個:

  1. 在情節提要板上找到初始視圖控制器和輔助視圖控制器。
  2. 按住Control鍵從初始視圖控制器頂部欄上的黃色圓圈拖動到輔助視圖。
  3. 從彈出菜單的“手動選擇”下,選擇正確的選項,可能是“顯示”。
  4. 單擊創建的segue,並為其指定一個標識符。
  5. 在您的代碼中,每當您要執行轉換時,都在UIViewController上調用performSegue(withIdentifier:sender:) (傳入您為segue指定的標識符字符串),其中包含最外面的集合視圖。

1.將segue從parentViewController連接到destinationViewController。

2.父視圖控制器

class parentViewcontroller : ViewController , ParentCellProtocol
{

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

    {
        Create instance of cell - parentCollectionViewCell
        //Set the delegate
        parentCollectionViewCell.parentDelegate = self
    }

    //Implement the ParentCellProtocol delegate method
    func buttonClickedFromParentCell()
    {
        //Perform segue here
    }
}

3.父自定義單元格

protocol ParentCellProtocol 
   {
      func buttonClickedFromParentCell()
   }

   class ParentCell : ChildCellProtocol
   {
      //Create an instance of protocol.
      var parentDelegate : ParentCellProtocol?

      //Implement ChildCellProtocol Method
      func buttonClickedFromChild(){

         parentDelegate.buttonClickedFromParentCell()
      }


      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
      {
         Create instance of cell - childCustomCell
         //Set the delegate
         childCustomCell. childDelegate = self
      }
   }

4.兒童自定義單元

protocol ChildCellProtocol {
    func buttonClickedFromChild()
}

class ChildCell
{
    //Create an instance of protocol.
    var childDelegate : ChildCellProtocol?

    //call the delegate method on button click.
    func toCustommenu(){

        //What can i do here
        childDelegate.buttonClickedFromChild()
    }
}

希望對您有幫助!

暫無
暫無

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

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