2016-05-13 16 views
0

メインクラス呼び出しnewsFeedCointrollerUICollectionViewControllerとして使用します。 1.セル内部では、私はFeedCellと呼ばれるクラスを使用するようなボタンを使ってニュースフィードを持っています。 2.メインビュー内のセルから抜け出す "スプラッシュメッセージ用にラベル(labelX) "function with" messageAnimated "セル内のボタンから外部関数を呼び出す方法

どのようにしてセル内のボタンから" messageAnimated "関数を呼び出すことができますか?

私は、たとえばにラベルテキストを変更したい:「あなたはそれを気に入って」...あなたのFeedCellでくれ

+0

あなたの「ボタン」とあなたの関数は、「messageAnimated」は –

答えて

2

を支援するための

おかげであなたは(デリゲートパターンについての記事を読むデリゲートを宣言する必要がありますhere

あなたのセルの実装では
protocol FeedCellDelegate { 
    func didClickButtonLikeInFeedCell(cell: FeedCell) 
} 

(手動でターゲットを追加することを想定)

var delegate: FeedCellDelegate? 

override func awakeFromNib() { 
    self.likeButton.addTarget(self, action: #selector(FeedCell.onClickButtonLike(_:)), forControlEvents: .TouchUpInside) 
} 

func onClickButtonLike(sender: UIButton) { 
     self.delegate?.didClickButtonLikeInFeedCell(self) 
} 
あなたのビューコントローラで

extension FeedViewController: UICollectionViewDataSource, UICollectionViewDelegate { 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell 
     // Do your setup. 
     // ... 
     // Then here, set the delegate 
     cell.delegate = self 
     return cell 
    } 

    // I don't care about other delegate functions, it's up to you. 
} 

extension FeedViewController: FeedCellDelegate { 
    func didClickButtonLikeInFeedCell(cell: FeedCell) { 
     // Do whatever you want to do when click the like button. 
     let indexPath = collectionView.indexPathForCell(cell) 
     print("Button like clicked from cell with indexPath \(indexPath)") 
     messageAnimated() 
    } 
} 
+0

がどこにあるか、私は私はここに新たなんだsoryだために、あなたは、いくつかのコードを置くことができますあなたは私の少しを助けることができます(もう一度、soryと私に助けてくれてありがとう) https://docs.google.com/document/d/1yGrYd6JFQU_xsqWEK_22H14nSTtc7TSUQxOFYtG-hp8/pub –

+0

よろしくお願いします。しかし、どうすればあなたを助けることができますか? – Tien

関連する問題