问题描述
我使用 Xcode 8 和 swift 3
我创建了一个新项目,在创建时选择了新的选项卡式应用程序".它提供了两个嵌入在一个标签栏控制器中的 UIViewController.我试图在视图控制器之间传递两个变量.
问题是从未调用准备转场功能.我在其中添加了一个从不打印的打印语句.我在两个视图控制器中都添加了准备功能,并且可以在不打印的情况下来回切换.
一些代码:
导入 UIKit导入 MapKit导入核心位置类 FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{@IBOutlet 弱变量 mapView:MKMapView!var locationManager:CLLocationManager?var currentLocation:CLLocation?var destPin:MapPin?var isTracking:布尔?= 假覆盖 func viewDidLoad() {super.viewDidLoad()locationManager = CLLocationManager()locationManager?.delegate = selflocationManager?.startUpdatingLocation()locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeterslocationManager?.requestAlwaysAuthorization()更新跟踪()}func locationManager(_ manager: CLLocationManager, didUpdateLocations 位置: [CLLocation]) {self.currentLocation = 位置 [0]}func locationManager(_ manager: CLLocationManager, didFailWithError 错误: 错误) {打印(错误)}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}func getMapView()->MKMapView{返回地图视图}覆盖 func viewWillAppear(_ 动画:布尔){super.viewWillAppear(true)如果(self.isTracking!){print("Istracking bool 为真")}别的{print("Istracking bool 为假")}更新跟踪()locationManager?.stopUpdatingLocation()}覆盖 func prepare(for segue: UIStoryboardSegue, sender: Any?) {打印(准备")让 barViewControllers = segue.destination 作为!UITabBarController让 destinationViewController = barViewControllers.viewControllers![1] as!第一视图控制器destinationViewController.destPin = self.destPindestinationViewController.isTracking = self.isTracking}}
prepare 函数的内容也很可能是错误的,但即使不调用该函数也无济于事.另一个视图控制器也嵌入在标签栏控制器中,也有一个不执行的prepare函数.
如果你愿意,它在这个 github
比如tabViewController
中嵌入了FirstViewController
和SecondViewController
:
在你的 firstViewController.swift
中:
导入 UIKit类 FirstViewController: UIViewController {var vc1_variable:String = "第一个 vc 的变量."覆盖 func viewDidLoad() {super.viewDidLoad()//在加载视图后做任何额外的设置,通常是从笔尖.}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()//处理任何可以重新创建的资源.}}
在你的 secondViewController.swift
中:
导入 UIKit类 SecondViewController: UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func viewWillAppear(_ 动画:布尔){super.viewWillAppear(动画)让 first_vc:FirstViewController = self.tabBarController?.viewControllers![0] as!第一视图控制器打印(\(first_vc.vc1_variable)")}}
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
It is easy to do:
The result:
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)")
}
}
这篇关于准备不使用 tabbarcontroller 调用 segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!