当我在两个 View Controller 之间切换时,我试图找到一种方法来保留 map View 的区域和缩放。
现在发生的情况是,每次我从 subview Controller 返回时, map View 都会重新初始化自身,因此 userTrackingMode 和 userLocation 也会重新初始化。
class MainViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
initLocationManager()
}
}
extension MainViewController: MKMapViewDelegate {
func initLocationManager() {
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.Follow, animated: true)
}
func stopLocationServices() {
mapView.setUserTrackingMode(.None, animated: true)
}
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
if mapViewRegionDidChangeFromUserInteraction() {
mapView.setUserTrackingMode(.None, animated: false)
}
}
func centerMapToUser() {
mapView.setUserTrackingMode(.Follow, animated: true)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MapAnnotation else {
return nil
}
return MapAnnotationView(annotation: annotation, reuseIdentifier: annotation.description)
}
}
是否有一个特定的segue可以防止父 View 的释放,或者我可能正在考虑存储所需的区域。
你觉得怎么样?
最佳答案
你可以尝试像这样的惰性实现。
@property (nonatomic,strong) MapView *mapView;
- (MapView *)mapView
{
if (!_mapView) {
_mapView = [MapView new];
}
return _mapView;
}
或者也许,只有将属性从弱变为强才能解决您的问题。 如果没有,请提供有关您的代码的更多信息。
关于ios - 如何防止mapview被释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411500/