2016-06-24 24 views
1

我有一個自定義的UITabBar控制器,其中我有按鈕可在視圖控制器之間切換。以下是圖像: http://i.stack.imgur.com/UInLI.png按下自定義UITabBarController上的按鈕時顯示用戶位置

我想這樣做時,按下主鍵視圖將更改到的MapViewController,當它再次按下(在相同的MapViewController),它會顯示出用戶的位置是什麼。

  • 我使用mapBox作爲我的地圖API,我有一個函數在我的MapViewController,

findUserLocation()

和這個節目的用戶位置使用:

mapView.userTrackingMode = MGLUserTrackingMode.Follow

所以在這裏是我的代碼:

class CustomTabBarViewController: UITabBarController { 

    override func viewDidLoad() { 
    super.viewDidLoad() 


    self.tabBar.hidden = true 

    let tabBarView = CustomTabBarView() 
    tabBarView.viewdidload() 
    self.view.addSubview(tabBarView) 
    tabBarView.mapButton.addTarget(self, action: #selector(self.changeToMyMap), forControlEvents: UIControlEvents.TouchUpInside) 
    // Do any additional setup after loading the view. 
} 

    func changeToMyMap(){ 
    if (self.selectedIndex != 0){ 
    self.selectedIndex = 0 
    }else{ 
    let mapViewController = myMap() // the class Name 
    mapViewController.mapView.userTrackingMode = MGLUserTrackingMode.Follow 
    } 
    } 
} 
  • 所以當我按下按鈕來顯示用戶的位置,我得到一個錯誤。我認爲是因爲Map未加載到customTabViewController中,但我不知道如何使其工作。

    當我嘗試從我的customTabBarController調用函數 時,我的應用程序出現致命錯誤。

fatal error: unexpectedly found nil while unwrapping an Optional value

+0

比幫助更多的信息。你能顯示你收到的錯誤嗎? – Xeaza

+0

我編輯我的文章,使其更清晰。 –

回答

0

如果你還沒有使用它們了,這些可能是一些helpful mapBox resources

我不知道爲什麼它會崩潰,你在那裏。
我會嘗試把一些斷點,以找出它實際上崩潰的地方。

試着把一個在線if (self.selectedIndex != 0){ 看看它是否真的在這個方法裏面。

如果是這樣,請嘗試在debug console中輸入po yourPropertyNamepo self.selectedIndex,以嘗試找出哪些屬性返回零。

另外,如果您知道哪些屬性是可選的,請檢查您訪問它們的位置。那就是他們可能是零的地方。

0

在我們的Swift應用程序中,我們利用showsUserLocation來切換用戶位置。

如果您只是試圖顯示用戶位置,這將值得嘗試。

Mapbox iOS SDK 3.2.3中的MGLUserTrackingMode用於如何跟蹤用戶在地圖上的位置(例如,駕車路線時的體驗)。

你怎麼能使用showsUserLocation

func changeToMyMap(){ 
    // not sure if the 'if/else' logic fit with what you intended, but 
    // left your code intact to better highlight how to use mapView.showsUserLocation 
    if (self.selectedIndex != 0){ 
    self.selectedIndex = 0 
     mapViewController.mapView.showsUserLocation = false 
    }else{ 
     let mapViewController = myMap() 
     // causes the map view to use the Core Location framework to find the current location 
     mapViewController.mapView.showsUserLocation = true 
    } 
    } 
} 

注意本說明中的文檔進行Mapbox iOS版SDK 3.2.3。您需要注意在.plist中設置新密鑰。

On iOS 8 and above, your app must specify a value for NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in its Info.plist to satisfy the requirements of the underlying Core Location framework when enabling this property.


enter image description here

對於我們如何使用showsUserLocation在Mapbox(什麼產生在上述GIF經驗)比較 - 我想我們會需要一點點

@IBAction func handleSettings(sender: AnyObject) { 

    var trackingLocation = UIImage(named: "TrackingLocationOffMask.png") 

    switch (self.mapView.userLocationVisible) { 
    case true: 
     self.mapView.showsUserLocation = false 
     trackingLocation = UIImage(named: "TrackingLocationOffMask.png") 
     break 
    case false: 
     self.mapView.showsUserLocation = true 
     trackingLocation = UIImage(named: "TrackingLocationMask.png") 
     break 
    } 

    self.navigationItem.leftBarButtonItem?.image = trackingLocation 
    } 
相關問題