Explorer
Explorer

Reputation: 75

How to push a view from inside a UITableViewCell that is in a UICollectionViewCell?

I have a UITableView inside of a UICollectionViewCell and I'm trying to push a new view from the UITableViewCell inside of the tableView(_:didSelectRowAt:) method. However, I cannot access navigationController. How would I go about navigating to the new view?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var item = Item(name: "")

    switch indexPath.section {
    case 0:
        item = array1![indexPath.item]
    case 1:
        item = array2![indexPath.item]
    default:
        break
    }

    let layout = UICollectionViewFlowLayout()
    let newView = NewCollectionView(collectionViewLayout: layout)
    newView.itemOfInterest = item

    // Can't reference navigationController
}

Upvotes: 0

Views: 439

Answers (3)

Amit
Amit

Reputation: 4886

One way is using NotificationCenter which you can write in ParentViewController :

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(methodNavigate(notification:)), name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: nil)
}

@objc func methodNavigate(notification: NSNotification) {
    Write your push segue code here.
}

Don't forget to remove observer in viewDidDisappear:

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: nil)
}

You can pass data through this too.

Then in didSelectRowAt :

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: "Send Data Here")

But I will suggest using protocol is much better and elegant way. You can get lots of tutorials and samples on internet:

Hope it helps. Happy coding.

Upvotes: 0

Jack
Jack

Reputation: 14329

Get parent controller of current View -

//MARK: get parent controller...
extension UIView {
    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
}

Usage:- Get navigationController reference as parentViewController?.navigationController

let viewController = GymLearnMoreViewController(nibName: "GymLearnMoreViewController", bundle: nil) //Your View controller instance
parentViewController?.navigationController?.pushViewController(viewController, animated: true)

Source Apple:-

https://developer.apple.com/documentation/uikit/uiresponder/1621099-next https://developer.apple.com/documentation/uikit/uiresponder?changes=_9

Upvotes: 1

ManuRaphy
ManuRaphy

Reputation: 391

for this

1 . You need to declare a protocol in the collectionviewcell where the tableview is added

2 . implement the protocol method on the class where the collectionview is added. and handle the navigation on click from here

3 . set the collectionview cell delegate when the collectionviewCellForrow at indexpath method is invoked and call the delegate method when the tableview item is clicked within the collectionview cell

Upvotes: 0

Related Questions