Just4School
Just4School

Reputation: 117

How to segue from a cell in xib to a VC in Storyboard

I'm creating an app with three table views (each in a different VC). I added everything to the main.storyboard, but I have to make one cell custom in a XIB file with three Labels. So my question is how can I add a segue from the cell in the XIB file to the ViewController in the Storyboard.

I saw that here's an answer but I don't understand this : Segue from Storyboard to XIB

Upvotes: 1

Views: 1768

Answers (2)

prex
prex

Reputation: 739

If you know the position of your customCell then you can easily achieve this programmatically by calling tableView delegate following ways: Suppose your custom cell is in the last index then:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard indexPath.row == models.count - 1 else {
        //behavior of other cell
        return
    }
    navigateToTestViewController()
}

But if you have a button in a customCell then you have to use delegate or reactive approach to send delegate to parentController to perform navigation.

Update: If you only need to make cell of type customCell navigable then you can do following way:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    guard cell is CustomCellTableViewCell else { return }
    navigateToTestViewController()
}

Upvotes: 2

MQLN
MQLN

Reputation: 2328

This question was answered quite nicely previously:

https://stackoverflow.com/a/44675813/4686915

here's the meat of it:

let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)
self.present(myViewController, animated: true, completion: nil)

or push in navigation controller

self.navigationController?.pushViewController(MyViewController(nibName: "MyViewController", bundle: nil), animated: true)

The only real difference is that you have to make sure that you're making the self... calls within your UIViewController.

Upvotes: 0

Related Questions