Place a button on the first VC as shown below.

// This is in the first VC
    func create_buttons (button_name: String) {
       // .............. omitted ......
Operation when the // button is pressed
        btn.addTarget (self, action: #selector (pushed), for: UIControlEvents.touchUpInside)
        // Add to screen
        view.addSubview (btn)

Press this button from there to change the screen.

@objc func pushed () {
        let storyboard: UIStoryboard = self.storyboard!
        let second = storyboard.instantiateViewController (withIdentifier: "next")
        self.present (second, animated: true, completion: nil)


Place a button on the destination screen and return to the previous screen from there.

@IBAction func backtomain (_ sender: Any) {
        self.performSegue (withIdentifier: "to_Mainpage", sender: nil)

When you return to the previous screen, the first button you have displayed disappears. Why is this? How can I fix it again?

  • Answer # 1

    Because "self.performSegue" is called in the processing in "backtomain", the same VC instance as the transition source screen is generated instead of the transition source screen, and the screen transition is performed. I think that it is in a state.
    If the screen transitions with self.present, I think I should go back with self.dismiss.