Anton Platonov
Anton Platonov

Reputation: 389

How to avoid calling viewDidLoad() to refresh the view

I am using swipe gesture to change the appearance of the main view:

@IBAction func changeSeqBack(_ recognizer: UISwipeGestureRecognizer) {

        if (recognizer.direction == UISwipeGestureRecognizerDirection.right) {
            count = 0
            if timerIndex % 5 == 0 {
                timerIndex -= 5
                initialState -= 1
                viewDidLoad()
            } else {
                while timerIndex % 5 != 0 {
                    timerIndex -= 1
                    if timerIndex % 5 == 0 {
                        count = 0
                        initialState -= 0
                        viewDidLoad()
                    } 
                }
            }
        }
    } 

The initialState variable, as it is changed with swipes, calls a number of methods which hide or show various objects on the screen. The problem is that I can only make the changes appear is calling viewDidLoad() after every swipe. That seems to be bad practice as it loads the memory. Is there any other approach to make it easy on the memory?

Upvotes: 1

Views: 117

Answers (1)

user7587085
user7587085

Reputation:

Create a new function for initial setup and move code inside viewDidLoad() to this function that you want to refresh and call the same from your viewDidLoad() and refresh function:

override viewDidLoad(){
    super.viewDidLoad()
    testFunction()
}

func testFunction() {
    // write your code here
}


if (recognizer.direction == UISwipeGestureRecognizerDirection.right) {
            count = 0
            if timerIndex % 5 == 0 {
                timerIndex -= 5
                initialState -= 1
                testFunction()
            } else {
                while timerIndex % 5 != 0 {
                    timerIndex -= 1
                    if timerIndex % 5 == 0 {
                        count = 0
                        initialState -= 0
                        testFunction()
                    } 
                }
            }
        }

Upvotes: 4

Related Questions