2

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.

CoderCoop
  • 79
  • 5

2 Answers2

1

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)
}
halfer
  • 19,824
  • 17
  • 99
  • 186
FrugalResolution
  • 568
  • 4
  • 18
  • Hey I appreciate it! I am now getting an error regarding unwrapping a nil for a UIImage for my users profile image that I was never getting before that present function. – CoderCoop Aug 04 '20 at 15:42
  • So now if the image if not loaded inside the `EditProfileTableViewController.viewDidLoad()`. Then you have to pass it from FristVC to SecondVC, do like: `let editProfileTableVC = EditProfileTableViewController()` `editProfileTableVC.profileImage = UIImage(named: String)` `present(editProfileTableVC, animated: true, completion: nil)` That's how you can set data to the new VC. The `var profileImage: UIImage!`must be created in EditProfileTableViewController class. – FrugalResolution Aug 05 '20 at 10:33
  • I have a function loading the Profile Image in the viewDidLoad(). I am not sure what is wrong. – CoderCoop Aug 05 '20 at 15:37
  • Send me your viewControllers and I'll have a look. My mall can be found under support of this link. [link](https://apps.apple.com/app/id1514415524) – FrugalResolution Aug 06 '20 at 12:00
0

Make sure, that the segue in the Storyboard has exactly the identifier: "EditProfile". Normally I'm writing identifiers with lower letter in the beginning. You also need to prepare for Segue. For example set the delegate:

  // Set ViewController class as the delegate of the EditProfileTableViewControllerDelegate protocol
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier {
      if identifier == "EditProfile" {
        let editProfileTableViewController = segue.destination as! EditProfileTableViewController
        editProfileTableViewController = self
      }
    }
  }

At one point in my coding time, I deleted all my storyboard because of too many error's that I could hardly solve. Now I'm doing it all programmatically. At first it was a bit hard to set up all the view's by myself but after all I'm very glad I'm not using storyboards anymore. For some stuff I need xib's, and for testing storyboard. Just if you're interested: the most iOS programmers are using storyboard, so it's okay if you go on with that. The advantage of doing it all programmatically is that there are no segue's anymore. So just present, and on navigation VC's push, pop, ...

FrugalResolution
  • 568
  • 4
  • 18
  • 1
    I'm getting the error, Cannot assign value of type 'NewNetworkProfilesViewController' to type 'EditProfileTableViewController' – CoderCoop Aug 03 '20 at 21:57
  • Looks like your Segue is pointing to a NewNetworkProfilesViewController but you try to get to the EditProfileTableViewController. Be sure that you are using unique identifiers. Also you should give each ViewController you are created on storyboard a own class. Like create a new File with the Class name: NewNetworkProfilesViewController.swift and EditProfileTableViewController.swift. Then go to storyboard and assign the classes to your ViewControllers. Then you should use as! EditProfileTableViewController instead of "as! UITableViewController". and write lower letters in variable names. let ed – FrugalResolution Aug 03 '20 at 22:06
  • I don't know how to make a segue programmatically. I changed the code before and should have updated. I will post the new edit now. – CoderCoop Aug 03 '20 at 22:08
  • Alright, looks better, now just after print("ButtonTapped") add the `performSegue(withIdentifier: "EditProfile", sender: self)`. Then write before `if let identifier = segue.identifier {` a print statement: `print("preparing for: \(segue.identifier)")` and see if it's shown up in the console when hitting the button. – FrugalResolution Aug 03 '20 at 22:14
  • And you don't need this line `editProfileTableViewController = self` – FrugalResolution Aug 03 '20 at 22:16
  • Does the function need to be inside or outside the Button Tapped function? – CoderCoop Aug 03 '20 at 22:27
  • Outside. The performSegue function is firstly calling the prepare function, then it's doing the segue in the case that the segue is created correctly with right identifier. Do you have zoom? You can share your screen and I could help you better... – FrugalResolution Aug 03 '20 at 22:34
  • Now I am getting the error: Use of unresolved identifier 'segue', inside the buttonfunction – CoderCoop Aug 03 '20 at 22:36
  • So go to your storyboard and your segue arrow. And in the identity inspector of this segue arrow give the identifier "editProfile". Press enter. After that make your code inside your ViewController look like this in the answer below: – FrugalResolution Aug 03 '20 at 22:45
  • 1
    I can't. This view controller is created programmatically. I don't have a story board for this segue. – CoderCoop Aug 03 '20 at 22:46
  • Then you don't need performSegue. You just need to `@objc func editProfileButtonAction(sender: UIButton!) { present(EditProfileTableViewController(), animated: true, completion: nil) }` – FrugalResolution Aug 03 '20 at 22:48
  • 1
    All my functions under that now are red saying there is no self – CoderCoop Aug 03 '20 at 22:55
  • 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. If this answers the question, please don't forget the upvotes please. – FrugalResolution Aug 03 '20 at 23:07
  • I've put a short explanation/summary for all of that for you on your other question: https://stackoverflow.com/a/63238393/12035498 – FrugalResolution Aug 03 '20 at 23:49