ios - 如何防止mapview被释放?

标签 ios swift mkmapview segue

当我在两个 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/

相关文章:

ios - 创建 iPad 的 viewController 的最佳方法是什么?

ios - 春萌要连动动画?

c# - 没有使用 Parse Push 和 Xamarin IOS 注册的设备

iOS:使用全屏图像显示模态视图 Controller

iphone - 在应用程序本身中转弯导航

ios - MapKit Xcode 编程

swift - 使用collectionView单元关闭 View Controller

根据大小写类型,Swift 将枚举返回为字符串或整数

ios - 按下按钮后如何显示 24 小时计数器

ios - 旋转后 MKAnnotationView 标注未正确放置