andrey
andrey

Reputation: 1165

How to navigate to another view controller by click on button in collection view cell

I have a collection view controller with several cells. Each cell have a button and I want to go navigate to another view controller by ckick button on collection view cell. I can do it by click on the cell, but I want to do by click on button in the cell, not by click on cell. I know how to do it by click on cell, for example:

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)
    }

It's simple because I have indexPath and can send like parameter.

How I try to do it:

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
}

but how to send book to my func goChapter outside didSelectItemAtIndexPath?

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)
}

Upvotes: 1

Views: 1653

Answers (2)

andrey
andrey

Reputation: 1165

I found the easier solution: in the function cellForItemAtIndexPath

cell.goButton.tag = indexPath.item
cell.goButton.addTarget(self, action: #selector(goChapter), forControlEvents: .TouchUpInside)

and then:

func goChapter(sender: UIButton!) {
    let layout = UICollectionViewFlowLayout()
    let bookChapterVC = BookChapterVC(collectionViewLayout: layout)
    bookChapterVC.chapter = book?.bookChapters![sender.tag]
    navigationController?.pushViewController(bookChapterVC, animated: true)
}

Upvotes: 0

Sohel L.
Sohel L.

Reputation: 9540

You can get the UIButton clicked index like this way:

func goChapter(sender: UIButton!) {
    var point : CGPoint = sender.convertPoint(CGPointZero, toView:collectionView)
    var indexPath = collectionView!.indexPathForItemAtPoint(point)
    bookChapterVC.chapter = self.book?.bookChapters![indexPath.row]
    navigationController?.pushViewController(bookChapterVC, animated: true)
}

Update your addTarget as below:

cell.goButton.addTarget(self, action: #selector(goChapter(_:)), forControlEvents: .TouchUpInside)

Hope this helps!

Upvotes: 1

Related Questions