如何防止mapview被解除分配?

时间:2016-05-24 10:54:40

标签: ios swift mkmapview segue

当我在两个视图控制器之间切换时,我试图找到一种方法来保留地图和缩放地图视图。

现在发生的事情是,每当我从子视图控制器返回时,mapview都会重新初始化,因此也会重新初始化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阻止了父视图的重新分配,或者我正在考虑存储所需的区域。

您怎么看?

1 个答案:

答案 0 :(得分:0)

你可以尝试像这样的懒惰实现。

@property (nonatomic,strong) MapView *mapView;

- (MapView *)mapView
{
    if (!_mapView) {
        _mapView = [MapView new];
    }
    return _mapView;
}

或许,只有将房产从弱房改为强房才能解决您的问题。 如果没有,请提供有关您的代码的更多信息。

相关问题