2016-12-17 61 views
0

即時通訊使用Xcode 8和swift 3準備for segue不被稱爲tabbarcontroller

我創建了一個新的項目,選擇新的「選項卡式應用程序」,當我創建它。它提供了嵌入到一個標籤欄控制器中的兩個UIViewController。我試圖在視圖控制器之間傳遞兩個變量。

問題是從不會調用準備segue函數。我在其中添加了一個從不打印的打印語句。我在兩個視圖控制器中都添加了準備功能,並且可以在沒有打印的情況下來回切換。

一些代碼:

import UIKit 
import MapKit 
import CoreLocation 

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{ 

@IBOutlet weak var mapView: MKMapView! 
var locationManager:CLLocationManager? 
var currentLocation:CLLocation? 
var destPin: MapPin? 
var isTracking: Bool? = false 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager = CLLocationManager() 
    locationManager?.delegate = self 
    locationManager?.startUpdatingLocation() 
    locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    locationManager?.requestAlwaysAuthorization() 
    updateTracking() 
} 



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    self.currentLocation = locations[0] 
} 
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
    print(error) 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 
func getMapView()-> MKMapView{ 
    return mapView 
} 
override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(true) 
    if(self.isTracking!){ 
     print("Istracking bool is true") 
    } 
    else{ 
     print("Istracking bool is false") 
    } 
    updateTracking() 
    locationManager?.stopUpdatingLocation() 

} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    print("Preparing") 

    let barViewControllers = segue.destination as! UITabBarController 
    let destinationViewController = barViewControllers.viewControllers![1] as! FirstViewController 
    destinationViewController.destPin = self.destPin 
    destinationViewController.isTracking = self.isTracking 
    } 
} 

的準備功能的內容很可能是錯的,以及,但甚至沒有被調用它的功能並不重要。另一個視圖控制器也嵌入在標籤欄控制器中,並且還具有不執行的準備功能。

如果你願意,它在這個github上repo

+0

你的意思是傳遞變量的'的TabBar controller'的'viewControllers'(兩個'ViewControllers'嵌入到標籤欄控制器)? – aircraft

+0

是的。 FirstViewController和SecondViewController嵌入在tabbarcontroller中。我希望在駐留在tabbarcontroller中的第一個和第二個視圖控制器之間傳遞變量。 – Khaines0625

回答

1

這是很容易做到:

結果:

the effect

例如,有FirstViewControllerSecondViewController嵌入式在tabViewController

在你的firstViewController.swift

import UIKit 

class FirstViewController: UIViewController { 

    var vc1_variable:String = "first vc's variable." 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

在你secondViewController.swift

import UIKit 

class SecondViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     let first_vc:FirstViewController = self.tabBarController?.viewControllers![0] as! FirstViewController 

     print("\(first_vc.vc1_variable)") 
    } 
} 
+0

非常感謝。我實現了它,它工作。我想在這種情況下,爲segue功能做準備並不是我想要的。 – Khaines0625