我正在创建类似于Apple Maps'这里讨论的底页: How to mimic iOS 10 maps bottom sheet
我创建了一个子视图infoViewController.xib (InfoViewController.swift - 代码和InfoViewController.xib - 视图设计) 此子视图将像Apple Maps示例一样显示在我的地图前面,因此您仍然可以看到它背后的地图。
mapView由MapViewController.swift控制,我使用以下代码从那里调用它:
func addInfoView() {
let infoVC = InfoViewController()
self.addChildViewController(infoVC)
self.view.addSubview(infoVC.view)
infoVC.didMove(toParentViewController: self)
}
我设置了一个方向'此视图上的按钮作为IBAction。按下此按钮时,我希望它创建方向并在MapViewController中的地图上显示它们。目标和当前位置信息通过UserDefaults传递给infoViewController。我有它'工作':
@IBAction func infoDirectionButton(_ sender: Any) {
/*here i have it getting current and destination locations*/
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mapViewController = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! MapViewController
self.present(mapViewController, animated: false)
//routeDirection Method is in MapViewController and handles the direction requests
mapViewController.routeDirections(current: current, destination: destination)
mapViewController.addInfoView()
}
这会导致屏幕上显示方向,但不会在屏幕闪烁之前重新加载我的mapView和infoView(子视图)。
有没有办法让它显示路线而不重新加载父视图和子视图。
自我存在'代码是必需的,所以我可以调用该ViewController中的方法,它也不会在地图上显示方向。
self.present(mapViewController, animated: false)
和addInfoView()在显示方向后重新加载子视图。否则它不会再显示
mapViewController.addInfoView()
道歉,如果这很难理解,我试图简化代码以突出问题。
答案 0 :(得分:0)
我想到的第一件事是你可以在找到和设置方向的地方添加通知。之后,您可以从控制器观察到包含地图的通知。
答案 1 :(得分:0)
根据@ B.Uyar的建议,我将NotificationCenter.default.post
实施到子视图上的路线按钮和父母的ViewDidLoad中的NotificationCenter.default.addObserver
。
我注意到通知观察者被多次调用(与我离开的次数相同并返回到地图屏幕)
我阅读了NSNotificationCenter calling two times,它告诉我在视图未被使用时我需要使用删除观察者。这样:
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "name"), object: nil)
}
这看起来像是按预期工作。这个主题可能总结了其他几个,但我认为它对于遵循类似思维过程的人来说可能仍然有用。