2014-09-23 67 views
2

我有一個谷歌地圖視圖在一個視圖控制器導航控制器,這是一個的TabBar控制器上的內部。一切正常,但我最初點擊地圖選項卡時,地圖上的加載時間範圍爲5-10秒。試圖預緊谷歌地圖查看

storyboard-layout

我遇到幾個StackOverflow的帖子,其中列出預加載標籤的方法如下:

for (UIViewController *viewController in self.tabBarController.viewControllers) 
{ 
    [viewController view]; 
} 

我就修改,以我的具體實現。

for (UIViewController *viewController in self.tabBarController.viewControllers) 
{ 
    UINavigationController *navCon = (UINavigationController*)viewController; 
    for (UIViewController *vc in navCon.viewControllers) { 
     if ([[NSString stringWithFormat:@"%@",vc.class] isEqual: @"MapViewController"]){ 
      MapViewController *mv = (MapViewController*) vc; 
      [mv view]; 
     } 

    } 
} 

不幸的是,這兩個實現都沒有預先加載map選項卡。

規格

  • 谷歌地圖SKD 1.7.2
  • 的iOS SDK 7.1

編輯 viewDidLoad中的MapViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:nil]; 
    mapView_.delegate = self; 
    AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate; 
    CLLocationCoordinate2D loc=appDelegate.locationManager.location.coordinate; 

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:loc.latitude 
                  longitude:loc.longitude 
                   zoom:12]; 
    [mapView_ setCamera:camera]; 
    mapView_.myLocationEnabled=YES; 
    mapView_.settings.myLocationButton = YES; 
    self.view = mapView_; 
} 

回答

4

我只是建議使用容器視圖(long how-to) ich很容易;它將可靠地獨立工作。如果你願意,只需在加載時將其移出屏幕(以後可能將其滑入)。

注意容器視圖內,說「父」是級BOSS的,

@implementation SomeContaineredView 
-(void)allTheDataLoaded 
    { 
    [(Boss*)self.parentViewController someMethodInBoss]; 
    } 
@end 

就是這麼簡單交談的父類。


注意 - 如果你需要從父母到containered視圖溝通,它很容易,如果你知道「傻招」,蘋果讓你做.. https://stackoverflow.com/a/15706092/294884

這是一個有點這樣的基本操作,你一直都很漂亮。你在prepareForSegue中做:像這樣...

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 
    if ([segue.identifier isEqualToString:@"containerLogin"]) 
     self.vcLogin = (LoginVC *)segue.destinationViewController; 

    if ([segue.identifier isEqualToString:@"containerStartNew"]) 
     self.vcStartNew = (StartNewVC *)segue.destinationViewController; 

    } 

在這個例子中有兩個容器視圖。 (使用標識符「containerLogin」和「containerStartNew」)所以,我有兩個屬性(self.vcLogin,self.vcStartNew)。這正是你設定它們的方式。

注意prepareForSegue是嚴重命名。它應該被稱爲類似於「設置它運行時,當你有一個嵌入segue」我在這裏詳細解釋:https://stackoverflow.com/a/24351813/294884

重要! ...

這裏有一個方便的宏!

#define seg(A, B, C) if ([segue.identifier isEqualToString:A]) \ 
          B = (C *)segue.destinationViewController; 

在我們處理的每個項目中,我們使用該宏。

然後,你可以簡單地寫:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 
    seg(@"cOverlayBuy", self.rockets, Rockets); 
    seg(@"cOverlayMainMenu", self.overlayMainMenu, OverlayMainMenu); 

    seg(@"cSearch", self.search, Search); 
    seg(@"cMeeting", self.meeting, Meeting); 

    seg(@"cMeetings", self.meetings, Meetings); 
    seg(@"cBooks", self.bikes, Bikes); 
    seg(@"cPeople", self.cars, Cars); 
    } 

因爲,這些天來,每一個場景有很多集裝箱的意見,每一個場景,我們有,對於每一個客戶,先後在「prepareForSegue」呼叫代碼。

所以,一旦代碼運行,你終於可以「訪問你的容器視圖!」

[self.cars displayColors:@"red"]; 
self.cars.view.hidden=YES; 
[self.meetings calculateNewTimesNow]; 

...等等。

就像我說的,我們在每個項目中都使用這個宏。幾乎每個場景現在都有幾個容器視圖,所以它在每個VC中!希望它能幫助別人。

+0

您是否已成功使用容器視圖作爲預加載視圖的策略?我承認我沒有玩過這個。 – eabraham 2014-09-25 17:48:26

+0

絕對 - 很多次。我們所做的只是複雜的客戶端 - 服務器應用。我通常更喜歡將它們保持在屏幕外,並在準備好時將它們滑入。 – Fattie 2014-09-25 17:51:09

+0

太棒了,我會試試看。 – eabraham 2014-09-25 17:57:25

0

我想你可以在該視圖之前實例化它,然後在加載該控制器時使其出現。如果你不能,也許你可以創建它遠離當前視圖,然後在其滿載時滑動它? (並使其看起來平滑)

+0

是的,我的問題包括在顯示之前實例化ViewController的代碼。我確認ViewController已經運行了它的ViewDidLoad並且它是同一個對象。不幸的是,加載谷歌地圖仍然需要5秒多的時間。 – eabraham 2014-09-25 17:33:58