2016-12-17 6 views
0

イムで呼び出されていないと私はそれを作成したときに、新しい「タブ付きアプリケーション」を選択し、新しいプロジェクトを作成しセグエの準備のXcode 8を使用してtabbarcontroller

3迅速。 1つのタブバーコントローラに2つのUIViewControllerが組み込まれています。私はビューコントローラ間で2つの変数を渡そうとしています。

問題は、segue関数の準備が呼び出されないことです。印刷されないprintステートメントを追加しました。両方のビューコントローラにprepare関数を追加し、プリントなしでタブを前後に移動できます。

いくつかのコード:

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

あなたは 'タブバーのcontroller'の' viewControllers'(2 'ViewControllers'タブバーコントローラに組み込まれた)の間で変数を渡す意味ですか? – aircraft

+0

はい。 FirstViewControllerとSecondViewControllerはtabbarcontrollerに組み込まれています。私はtabbarcontrollerに存在する最初のビューコントローラと2番目のビューコントローラの間で変数を渡したいと思います。 – Khaines0625

答えて

1

をたい場合はそれが行うのは簡単です:

結果:たとえば

the effect

FirstViewControllerSecondViewController埋め込まがありますin tabViewController

お客様のfirstViewController.swift

あなた secondViewController.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. 
    } 


} 

:?

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

関連する問題