私はいくつかのセルを持つコレクションビューコントローラを持っています。各セルにはボタンがあり、コレクションビューセルのckickボタンで別のビューコントローラに移動したいと思っています。私はセルをクリックすることでそれを行うことができますが、セルをクリックするのではなく、セル内のボタンをクリックして行います。 私は例えば、セルをクリックしてそれを行う方法を知っている:コレクションビューのセルのボタンをクリックして別のビューコントローラに移動する方法
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let book = books?[indexPath.item] {
showBookDetail(book)
}
}
func showBookDetail(book: Book) {
let layout = UICollectionViewFlowLayout()
let bookDetailVC = BookDetailVC(collectionViewLayout: layout)
bookDetailVC.book = book
navigationController?.pushViewController(bookDetailVC, animated: true)
}
私はindexPath
を持っており、パラメータと同様に送信することができますので、それは簡単です。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! BookCell
cell.chapter = books?.bookChapters?[indexPath.item]
cell.goButton.addTarget(self, action: #selector(goChapter), forControlEvents: .TouchUpInside)
return cell
}
をどのように私のFUNC goChapter
外didSelectItemAtIndexPath
にbook
を送信するために:
どのように私はそれをしようか?あなたはUIButton
は、この方法のようにインデックスをクリック得ることができます
func goChapter() {
let layout = UICollectionViewFlowLayout()
let bookChapterVC = BookChapterVC(collectionViewLayout: layout)
bookChapterVC.chapter = self.book?.bookChapters![0] // here I want to send separate chapter of the book
navigationController?.pushViewController(bookChapterVC, animated: true)
}
をはい、それは動作します。ありがとう! – andrey