If you put returnToView
into your viewDidLoad
it will not be executed because at the time viewDidLoad
gets called, the view controller itself has not fully setup yet and it’s not visible on the window hierarchy. So you cannot use present
. You may think to put it in viewWillAppear
or viewDidAppear
. However, it has a drawback: whenever you back from another present
or back from navigationController.pushViewController
, it will get called again.
So, I think this will achieve your goal. It works on a single instance of ViewController
.
class ViewController: UIViewController {
private var isExecuteSaveFunc = false
@objc func returnToView(_ sender: UIButton) {
let vsdd = SavedCanvasViewController()
if !isExecuteSaveFunc {
savedFunc()
isExecuteSaveFunc = true
}
vsdd.modalPresentationStyle = .fullScreen
vsdd.modalTransitionStyle = .coverVertical
present(vsdd, animated: true)
}
}
In case you want savedFunc
to execute once during the lifetime of the app, I think UserDefaults
will fit.