Search code examples
iosswifttableviewsegue

How to segue into a new view controller from button in table view cell


How can I segue into a new view controller from a button that is in a table view cell? I need to send some data with it so I'm not sure if I can do that from my tableViewCell class.


Solution

  • try

    var profilePressed: ((UITableViewCell) -> Void)?
    
    @IBAction func profileNamePressed(_ sender: Any) {
        profilePressed?(self)
    }
    

    in your table view cell class, and this:

    cell.profilePressed = { (cell) in
        let profileVC = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC") as! ProfileVC
        profileVC.initData(withPostedBy: message.postedBy)
        self.presentDetail(profileVC)
    }
    

    in your cell for row at index path function