我有一个scrollView,其中包含动态数量的WeatherViewController,每个WeatherViewController显示用户已保存的不同城市的天气数据。用户可以从WeatherViewControllers筛选到CityListViewController。他们可以在其中从列表中添加和删除城市,而在解散CityListViewController之后又应从scrollView中添加和删除WeatherViewControllers,这就是我遇到的问题。
<func createAndAddWeatherScreen被称为更改和所有更改之后的准确时间,如果我关闭该应用程序并重新打开它,则scrollView然后将显示正确数量的viewControllers。似乎一切正常,只是在关闭cityListController时未重新加载scrollView。
侧注:最初打开应用程序时,scrollView会正确加载,并带有UIScrollView中的所有正确WeatherWeatherController和列表中的正确城市。
class ScrollViewController: UIViewController, ScrollReloadProtocol {
func reloadScrollView() {
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
let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.isPagingEnabled = true
return scrollView
}()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
view = scrollView
//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
scrollView.addSubview(weatherScreen.view)
addChild(weatherScreen)
weatherScreen.didMove(toParent: self)
return weatherScreen
}
viewDidLoad
方法,很容易提取必要的代码以进行提取和重新加载到自己的方法中,而这不会破坏viewController
的生命周期。scrollView
用作tableView
,它提供了作为免费功能的重装,以及更好的内存管理。.map()
而不是使用while
来手动迭代和创建新数组,高阶函数非常有用。scrollView
那样重新加载tableView
。它已经包含了您之前在其中添加的视图,在我看来,就像您一次又一次地堆叠额外的视图一样,如果使用可视调试器,则可以进行调试。最后,为了在更新约束后更新视图,您需要调用:
setNeedsLayout()
layoutIfNeeded()
让系统知道您要更新子视图及其布局。