Search code examples
swiftxcodetableviewsegue

Make a segue to a tableview controller from a view controller


I am trying to make a segue to a table view controller when a button is tapped in my view controller programmatically. Here is my code:

@objc func editProfileButtonAction(sender: UIButton!) {
    print("ButtonTapped")
    
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let identifier = segue.identifier {
        if identifier == "EditProfile" {
            var editProfileTableViewController = segue.destination as! EditProfileTableViewController
          editProfileTableViewController = self
        }
      }
    }
    
}

I really could use some help. I also need to make a segue to a collection view controller using a button in the same view controller.


Solution

  • Okay to clarify that. There is no way to create a segue programmatically. Segues are the arrows on storyboard linking from one to another VC. They are called with: performSegue. This calls the function prepare.

    If you want to show a new VC when hitting a button (without segue), then you use the present(VC(), animated: true, completion: nil) } inside the button function. The VC is presented modally.

    @objc func editProfileButtonAction(sender: UIButton!) {
        print("editProfileButtonAction")
        present(EditProfileTableViewController(), animated: true, completion: nil)
    }