如何将包含 ViewController 的 UIScrollView 重新加载到 add/remove 视图控制器

How do I reload UIScrollView containing ViewControllers to add/remove view controllers

我有一个滚动视图,其中包含动态数量的 WeatherViewController,每个显示用户保存的不同城市的天气数据。用户可以从 WeatherViewControllers 转到 CityListViewController。他们可以在他们的列表中添加和删除城市的地方,这反过来应该在关闭 CityListViewController 时从 scrollView 添加和删除 WeatherViewControllers,这就是我 运行 遇到问题的地方。

目前,我正在尝试使用协议在关闭 CityListViewController 时在 scrollViewController 中调用 viewDidLoad,但出现错误:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file)

当它到达:

let weatherScreen = storyboard!.instantiateViewController(identifier: "View Controller") as! ViewController

旁注: 最初打开应用程序时,scrollView 会正确加载 UIScrollView 中所有正确的 WeatherViewController 和列表中的正确城市。

class ScrollViewController: UIViewController, ScrollReloadProtocol {

    func reloadScrollView() {

        print("SCROLL RELOADED!!!!!*******")
        self.viewDidLoad()
    }

    @IBOutlet weak var totalScrollView: UIScrollView!
    var pages = [ViewController]()
    var x = 0
    var weatherScreensArray = [SavedCityEntity]()
    var weatherScreenStringArray = [String]()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    var horizString = "H:|[page1(==view)]"

    let defaults = UserDefaults.standard


    override func viewDidLoad() {
        super.viewDidLoad()

        //userDefaults used to keep track of which screen is which to put different cities on different viewControllers
        defaults.set(0, forKey: "screenNumber")
        //load cities to get number of cities saved
        loadCities()

        var views : [String: UIView] = ["view": view]
        //create all weatherWeatherControllers
        while x <= weatherScreensArray.count {

            pages.append(createAndAddWeatherScreen(number: x))
            weatherScreenStringArray.append("page\(x+1)")
            views["\(weatherScreenStringArray[x])"] = pages[x].view
            let addToHoriz = "[\(weatherScreenStringArray[x])(==view)]"
            horizString.append(addToHoriz)

            x+=1
        }

        horizString.append("|")

        let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[page1(==view)]|", options: [], metrics: nil, views: views)
        let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: horizString, options: [.alignAllTop, .alignAllBottom], metrics: nil, views: views)

        NSLayoutConstraint.activate(verticalConstraints + horizontalConstraints)
    }

    //Function to create and add weatherViewController
    func createAndAddWeatherScreen(number: Int) -> ViewController {

            defaults.set(number, forKey: "screenNumber")

            let weatherScreen = storyboard!.instantiateViewController(identifier: "View Controller") as! ViewController

            weatherScreen.view.translatesAutoresizingMaskIntoConstraints = false

            totalScrollView.addSubview(weatherScreen.view)

            addChild(weatherScreen)
            weatherScreen.didMove(toParent: self)

        return weatherScreen
    }
}

您可以尝试这样的操作:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let weatherScreen = storyboard.instantiateViewController(withIdentifier:  "View Controller") as! ViewController

最好还是用一个词作为标识符:"ViewController",并给它一个与 class 相同的名称。确保也在您的实际情节提要中设置这些值。