0

Every time a button is pressed, I am trying to present another view controller on top of it. I used this answer, and it works when I press the button for the 1st time. However, when I press the button for the 2nd/3rd/etc. time, the view controller does not appear and the print statement does not print either. Any help would be greatly appreciated!

Here is my code:

@IBAction func myButtonPressed(_ sender: Any) {
        print("button pressed")

        var vc = storyboard!.instantiateViewController(withIdentifier: "myIdentifier") as! secondViewController
        var transparentGrey=UIColor(red: 0.26, green: 0.26, blue: 0.26, alpha: 0.90)
         vc.view.backgroundColor = transparentGrey
         vc.modalPresentationStyle = .overCurrentContext
        present(vc, animated: true, completion: nil)
}

EDIT: The above code works as long as you dismiss the secondViewController correctly after the button was first pressed. I put this code in the secondViewController and it worked for me:

navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
Audrey
  • 165
  • 1
  • 1
  • 9

0 Answers0