prepare for segue not being called with tabbarcont

2019-09-06 19:15发布

Im using Xcode 8 and swift 3

I created a new project selecting new "Tabbed Application" when i created it. It provided two UIViewControllers embedded in one tab bar controller. I'm trying to pass two variables between the view controllers.

The problem is that the prepare for segue function is never called. I added a print statement in it that never prints. I added the prepare function in both view controllers and can tab back and forth with no prints.

Some code:

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
    }
}

The contents of the prepare function may very well be wrong as well but without the function even being called it hardly matters. The other view controller is also embedded in the tab bar controller and also has a prepare function that does not execute.

If you'd like, its in this github repo

标签: ios swift3
1条回答
姐就是有狂的资本
2楼-- · 2019-09-06 19:21

It is easy to do:

The result:

the effect

For example, there are FirstViewController and SecondViewController embeded in tabViewController:

In your 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.
    }


}

In your 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)")
    }
}
查看更多
登录 后发表回答