Adding a new page into PageViewController

Viewed 820

I am a Swift noob and am making a simple weather app. I used the Page-Based Application template.

The problem I have is the following:

When the user adds a city I call addCity and successfully append the new city name to my cities array. When I print that array in that function, it shows the new city at the end.

However, the function viewControllerAtIndex that creates a new page seems to use the old version of that array, without the new city appended. When I print the cities array, it is missing the new city name. Therefore, when the users swipes there is won't be a new page for the new city rendered. The user has to restart the app in order for the new city to show up.

I created a screen capture video to illustrate the problem.

https://youtu.be/DbMqgJ0lONk

(the cities array should also show "London", I think I just didn't restart the app)

I would appreciate any help here!

import UIKit

class ModelController: NSObject, UIPageViewControllerDataSource {

    var rootViewController = RootViewController()
    var cities = [""]
    let defaults = UserDefaults.standard

    override init() {
        super.init()

        self.cities = self.defaults.stringArray(forKey: "SavedStringArray") ?? [String]()

        if self.cities == [""] || self.cities.count == 0  {
            self.cities = ["Current Location"]
        }
    }

    func addCity(name:String) {
        self.cities.append(name)
        self.defaults.set(self.cities, forKey: "SavedStringArray")
        print ("cities from addCity:")
        print (self.cities)

    }


    func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> DataViewController? {
        // Return the data view controller for the given index.
        if (self.cities.count == 0) || (index >= self.cities.count) {
            return nil
        }

        // Create a new view controller and pass suitable data.
        let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! DataViewController

        //get city name
        dataViewController.dataObject = self.cities[index]
        print ("cities in viewControllerAtIndex:")
        print (self.cities)

        return dataViewController
    }



    func indexOfViewController(_ viewController: DataViewController) -> Int {
        // Return the index of the given data view controller.
        // For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
        return self.cities.index(of: viewController.dataObject) ?? NSNotFound
    }


    // MARK: - Page View Controller Data Source

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var index = self.indexOfViewController(viewController as! DataViewController)
        if (index == 0) || (index == NSNotFound) {
            return nil
        }

        index -= 1
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = self.indexOfViewController(viewController as! DataViewController)
        if index == NSNotFound {
            return nil
        }

        index += 1
        if index == self.cities.count {
            return nil
        }
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

}
1 Answers
Related