2016-11-10 4 views
0

UICollectionViewをUICollectionViewCellから参照できますか? 最後に、UICollectionViewCellからViewControllerを参照したいと思います。UICollectionViewCellからUICollectionViewを参照する方法は?

以下のコードは、私のCustomCollectionViewCellクラスにあります。 このクラスのツイートサポートをアーカイブしたいと思います。

@IBAction func tweetBtnTapped(_ sender: Any) { 
    let cvc = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 
    if let c = cvc { 
     c.setInitialText("test tweet from iOS App") 

     ### How can I refer ViewController?? 
     viewController = ??????? 

     if let vc = viewController { 
      vc.present(c, animated: true, completion: nil) 

     } 
    } 
} 

答えて

0

コードサンプルまたは達成しようとしているものを提供してください。私が理解していることは、あなたのセルで何らかのアクションを実行するときに、あなたのView Controllerに何らかのアクションを戻したいということです。私は正しいですか?だから、カスタム・コレクション・ビューのセル、コメントを1として

クラスCustomeCell:

輸入のUIKit

:UICollectionViewCell {

@IBAction func tweetButtonPressed() { 

    let cvc = SLComposeViewController(forServiceType:SLServiceTypeTwitter) 
    if let c = cvc { 
     c.setInitialText("test tweet from iOS App") 

     self.parentViewController?.presentViewController(c, animated: true, completion: nil) 
    } 

} 

}

Extension.Swiftファイルを作成します。

拡張UIView {

var parentViewController: UIViewController? { 

    var parentResponder: UIResponder? = self 

    while parentResponder != nil { 
     parentResponder = parentResponder!.nextResponder() 

     if let viewController = parentResponder as? UIViewController { 

      return viewController 

     } 

    } 

    return nil 

} 

}

これは私のために働いています。ではごきげんよう!

+0

ご意見ありがとうございます。私は私の質問を更新しました。 –

+0

委任を使用することもできます。したがって、セル内のボタンをタップすると、View Controllerに委任してソーシャルVCを提示することができます。または、UIVIewの拡張機能を作成して親コントローラを取得し、それをカスタムセルクラス内で使用してViewコントローラを表示することもできます。私は個人的に委任を使用してそれを行います。新しいコントローラを提示するためのコードをセルに持たせるべきではないので、拡張機能を使用してソリューションを投稿しています。 – Ashish

+0

これは私のコレクションビューのセルクラスです。 – Ashish

関連する問題