About the screen transition of the UIViewController Every time I transition the screen, it consumes memory.

Asked 2 years ago, Updated 2 years ago, 39 views

If you want to go back one, self.dismissViewControllerAnimated(true, completion:nil) will deinit the closed screen, or if you want to go back two, it will deinit.self.presentingViewController?presentingViewController,?presentingViewControllerAnimated(trimiss)

I'm sorry for the rudimentary question, but I've been an amateur for about two weeks since I started developing Swift's program (smartphone application).

Please let me know

The situation is in the case of the screen transition as shown below, and when returning from screen 4 (failure screen) to screen 2,
I would like to discard the instance on screen 2 and execute the initial process.
--------------------------------------------------------
Screen 1 (Start Screen) → Screen 2 (Main Screen) 画面 → Screen 3 (Success Screen)
                      ┠ → Screen 4 (Failure Screen)
—————————————————————————————————————————————————————————————————————————————

Screen 4 has buttons Yes and No
If Yes is selected, go to screen 2
"If ""No"" is selected, I would like to move to screen 1 respectively."

// When selecting "Yes", if you execute the code below
self.dismissViewControllerAnimated (true, completion:nil)
// If you discard the instance of screen 4 and return to screen 2, of course it will remain as it was last time
I don't run //viewDidLoad and go back to screen 2.
// Is it possible to release the instance of screen 2 when transitioning from screen 2 to failure screen?
// I would like to initialize the contents of the screen when I move to screen 2.

// Run the following code when selecting "No"
// Discard the instance of screen 4 and return to screen 1 (screen 4 and screen 2 both appear to be deinit)
self.presentingViewController?presentingViewController?dismissViewControllerAnimated (true, completion:nil)

swift

2022-09-30 21:13

1 Answers

It's a bit rough to release the View Controller from memory and create the same View Controller because you want to initialize the screen.Just before returning from the modal, you can return the items on the View Controller to the starting state, which will not cause any problems.

For example...
The class of View Controller on the modal calling side is ViewController.ViewController has three UITextField and when you press the OK button on the modal View Controller, erase the text entered in the three UITextField.

class ViewController:UIViewController {

    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
    @IBOutlet weak var textField3: UITextField!

    override func viewDidLoad(){
        super.viewDidLoad()

        clearValues()
    }

    // Return items to the starting state.
    func clearValues() {
        textField1.text=""
        textField2.text=""
        textField3.text=""
    }

    ・・・・・・・

}

The modal View Controller OK button action method dismissWithOK() is implemented as follows:

@IBAction funcdisWithOK(sender:AnyObject){
    iflet controller=self.presentingViewController as ?ViewController {
        controller.clearValues()
        controller.dismissViewControllerAnimated(true, completion:nil)
    }
}


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.