我有一个scrollView,其中包含动态数量的WeatherViewController,每个WeatherViewController显示用户已保存的不同城市的天气数据。用户可以从WeatherViewControllers筛选到CityListViewController。他们可以在其中从列表中添加和删除城市,而在解散CityListViewController后又应从scrollView中添加和删除WeatherViewControllers,这就是我遇到的问题。
目前,我正在尝试使用协议调用func reloadScrollView,该函数在关闭(viewDidDisappear)CityListViewController后在scrollViewController中调用viewDidLoad,但出现错误:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file
何时到达:
totalScrollView.addSubview(weatherScreen.view)
使用调试器,我发现totalScrollView为nil,这导致了问题。有没有一种方法可以使scrollView加载,因此在关闭另一个viewController时它不会为
OR
使用该协议调用此函数的更好时机吗?
侧面注意:最初打开该应用程序时,scrollView会正确加载,并且UIScrollView中的所有正确WeatherWeatherController以及列表中正确的城市。
class ScrollViewController: UIViewController, ScrollReloadProtocol {
func reloadScrollView() {
print("SCROLL RELOADED!!!!!*******")
self.viewDidLoad()
}
@IBOutlet 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 story = UIStoryboard(name: "Main", bundle: nil)
let weatherScreen = story.instantiateViewController(identifier: "View Controller") as! ViewController
weatherScreen.view.translatesAutoresizingMaskIntoConstraints = false
totalScrollView.addSubview(weatherScreen.view)
addChild(weatherScreen)
weatherScreen.didMove(toParent: self)
return weatherScreen
}
}
答案 0 :(得分:0)
跳过一个事实,那就是您做错了,让我们一次讨论一个问题。您尝试在totalScrollView
中隐式访问viewDidLoad
,如果插座已链接,则应在此时加载。如果是nil
,则应该: