【问题标题】:Preserve state of GMSMapView when using custom segue使用自定义 segue 时保留 GMSMapView 的状态
【发布时间】:2014-05-25 07:41:23
【问题描述】:

目前,我使用SWRevealViewController 为我的项目创建侧边栏和google map api

https://github.com/John-Lluch/SWRevealViewController

这就是问题所在。每次我使用侧边栏在ViewControllers 之间切换并返回到包含GMSMapViewMapViewController 时,GMSMapView 都会重新加载到我的位置(我在viewDidLoad 方法中设置它)。

如何防止MapViewController 每次都像这样重新加载?


到目前为止我已经尝试过: 使 MapViewController 成为单例并将其设置为 destinationViewController 但它不起作用。我得到的只是黑屏。

我的custom segue 代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] )
    {
        SWRevealViewControllerSegue* rvcs = (SWRevealViewControllerSegue*) segue;
        SWRevealViewController* rvc = self.revealViewController;

        rvcs.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
        {
            if ([segue.identifier isEqualToString:@"SegueToMapViewController"])
            {
                dvc = [MapViewController sharedMap]; //singleton
            }
            UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:dvc];
            [nc setNavigationBarHidden:YES];
            [nc setToolbarHidden:YES];
            [rvc setFrontViewController:nc animated:YES];
        };
    }
}

【问题讨论】:

    标签: ios objective-c gmsmapview swrevealviewcontroller


    【解决方案1】:

    确保您不要更改 viewWillAppearviewDidAppear 中的地图摄像头位置。

    如果不是这样,您可以在离开视图控制器之前保存相机位置,并在控制器再次显示时恢复它。

    【讨论】:

    • 我会解决这个问题,但这有点像解决问题的棘手方法,因为GMSMapView 仍然会重新加载地图数据,因此会耗费时间和用户移动数据。
    【解决方案2】:

    您的问题已经有一段时间了,但是这些天我遇到了类似的问题,我想我找到了保留GMSMapView 状态的方法(尽管我不知道SWRevealViewController 是否会进行任何更改)。

    正如你所说,我们的想法是有一个“单例”,但我不是为GMSMapView 制作的控制器。尝试将GMSMapView 设为根UINavigationController 的属性,方法是创建它的子类并在子类的ViewDidLoad 上初始化映射。

    界面:

    #import <UIKit/UIKit.h>
    #import <GoogleMaps/GoogleMaps.h>
    
    @interface TEST1NavigationController : UINavigationController
    
    @property GMSMapView *mapView;
    
    @end
    

    类:

    #import "TEST1NavigationController.h"
    
    ...
    
    @implementation TEST1NavigationController
    
        - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.7056308
                                                           longitude:-73.9780035
                                                                zoom:15];
    
        self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
        self.mapView.myLocationEnabled = YES;
    }
    
    ...
    

    然后,在每个单独的控制器中,您可以向自定义导航控制器询问 GMSMapView 并使用它。

    #import "TESTMAP1ViewController.h"
    #import "TEST1NavigationController.h"
    
    @interface TESTMAP1ViewController ()
    
    @end
    
    @implementation TESTMAP1ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        //Retrieve the mapView and assign it to the main view
        self.view = [self mapView];
    
        // Creates a marker in the center of the map.
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.position = CLLocationCoordinate2DMake(40.7056308,-73.9780035);
        marker.title = @"Sometitle";
        marker.snippet = @"Sometext";
        marker.map = [self mapView];
    
    }
    
    -(GMSMapView*)mapView{
        //Returns the view from the Navigation Controller
        return [((TEST1NavigationController*)[self navigationController]) mapView];
    }
    
    ...
    

    这样状态应该保存在导航控制器的控制器中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多