Search code examples
swiftbuttonuinavigationcontrolleruinavigationbarlevels

How to update information on previous page of navigation controller without relaunching page?


I am making a quiz app and have three view controllers and I am also making use of a navigation controller. The controllers are: HomeController, LevelsController, PlayController. The levels controller has 100 levels on it as buttons and all of them except level 1 are greyed out initially. As I complete levels, the other level buttons are unlocked and become clickable.

MY ISSUE: This only works if I go back to the HomeController and press on the then navigate to the LevelsController again, it doesn't update instantly when I simply go back from the PlayController to the LevelsController.

This is my code in the levels controller:

import UIKit

class LevelsViewController: UIViewController {



    var levels = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()

        for k in globalScore...99 {
            // print("this is \(k)")
            // print(levelButton)
            levelButton[k].isEnabled = false
            print("Have the levels updated??")
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        for k in globalScore...99 {
            levelButton[k].isEnabled = false
        }
    }

    @IBOutlet var levelButton: [UIButton]!

    @IBAction func levelSelect(_ sender: UIButton) {
        self.level = sender.tag
        performSegue(withIdentifier: "LevelToPlaySegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        print("my name is jacobson")
        var vc2 = segue.destination as! PlayViewController
        vc2.enteredLevel = self.level
    }
}

My PlayController has a keyboard and an image that changes to a new image if the correct answer is input into a text field. I also have a score in the top right of the PlayController which is assigned to globalScore. as the global score increases, the levels page should have more levels available to the user when they go backwards from the PlayController to the LevelsController.


Solution

  • What you need to do is to change your logic and instead of using it on viewDidLoad, you should also add ViewDidAppear and ViewWillAppear, in order to call these method every time you open the viewController instead of doing it only the first time.