以编程方式切换 tabBar 导致 IBOutlet 为 nil
Programmatically switching tabBar causes IBOutlet to be nil
我正在以编程方式切换 tabBar - 效果很好。但是,IBOutlet mapView(google 地图)变为 nil - 导致崩溃...
我在这上面花了好几个小时。感觉就像我错过了一些微不足道的东西。通过SO查看,例如:
和 All my IBOutlet are nil in viewDidLoad 但没有运气。
非常感谢任何帮助!
应用委托
func goToTabRoot(){
if let tabBarController = self.window!.rootViewController as? UITabBarController {
let index = 0 // First (root) tab
tabBarController.selectedIndex = index
let vc = tabBarController.viewControllers![index] as! UINavigationController
tabBarController.delegate?.tabBarController!(tabBarController, didSelectViewController:vc)
}
}
地图视图控制器
class MapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, BudgetTableViewControllerDelegate, StoreViewControllerDelegate, ProductPickedViewControllerDelegate {
@IBOutlet weak var mapView: GMSMapView!
override func viewDidAppear(animated: Bool) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest // Best accuracy
locationManager.requestWhenInUseAuthorization()
mapView.delegate = self // <== ERROR HERE. mapView = nil
println("ViewDidAppear called")
}
func favoriteViewDidFinish(controller: MapViewController, productIds: [Product]) {
println("Favorite finished")
// Select MapView
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.goToTabRoot()
// Store product IDs
self.productIds = productIds
// Update stores (download)
queryType = "filterProducts"
downloadStores = true
updateStores()
}
已选择的产品
protocol ProductPickedViewControllerDelegate: class {
func favoriteViewDidFinish(controller: MapViewController, productIds: [Product])
}
class ProductPickedViewController: UIViewController, UITabBarControllerDelegate {
var delegate:ProductPickedViewControllerDelegate? = nil
@IBAction func storesButtonPressed(sender: AnyObject) {
// Set delegate
delegate = MapViewController() // First view controller
// Download relevant stores
println("Should reload stores...")
if (self.delegate != nil) {
println("activate delegate")
self.delegate!.favoriteViewDidFinish(MapViewController(), productIds: productsPresented)
}
问题是当您使用以下行设置委托时,您正在创建 MapViewController
的新实例。该实例未在情节提要中创建,因此它对您的 IBOutlet
.
一无所知
delegate = MapViewController()
您需要获取对已存在控制器的引用。如果 ProductPickedViewController
是标签栏控制器中的视图控制器之一,你可以这样做,
delegate = self.tabBarController!.viewControllers[0] as! MapViewController
我正在以编程方式切换 tabBar - 效果很好。但是,IBOutlet mapView(google 地图)变为 nil - 导致崩溃...
我在这上面花了好几个小时。感觉就像我错过了一些微不足道的东西。通过SO查看,例如:
非常感谢任何帮助!
应用委托
func goToTabRoot(){
if let tabBarController = self.window!.rootViewController as? UITabBarController {
let index = 0 // First (root) tab
tabBarController.selectedIndex = index
let vc = tabBarController.viewControllers![index] as! UINavigationController
tabBarController.delegate?.tabBarController!(tabBarController, didSelectViewController:vc)
}
}
地图视图控制器
class MapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, BudgetTableViewControllerDelegate, StoreViewControllerDelegate, ProductPickedViewControllerDelegate {
@IBOutlet weak var mapView: GMSMapView!
override func viewDidAppear(animated: Bool) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest // Best accuracy
locationManager.requestWhenInUseAuthorization()
mapView.delegate = self // <== ERROR HERE. mapView = nil
println("ViewDidAppear called")
}
func favoriteViewDidFinish(controller: MapViewController, productIds: [Product]) {
println("Favorite finished")
// Select MapView
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.goToTabRoot()
// Store product IDs
self.productIds = productIds
// Update stores (download)
queryType = "filterProducts"
downloadStores = true
updateStores()
}
已选择的产品
protocol ProductPickedViewControllerDelegate: class {
func favoriteViewDidFinish(controller: MapViewController, productIds: [Product])
}
class ProductPickedViewController: UIViewController, UITabBarControllerDelegate {
var delegate:ProductPickedViewControllerDelegate? = nil
@IBAction func storesButtonPressed(sender: AnyObject) {
// Set delegate
delegate = MapViewController() // First view controller
// Download relevant stores
println("Should reload stores...")
if (self.delegate != nil) {
println("activate delegate")
self.delegate!.favoriteViewDidFinish(MapViewController(), productIds: productsPresented)
}
问题是当您使用以下行设置委托时,您正在创建 MapViewController
的新实例。该实例未在情节提要中创建,因此它对您的 IBOutlet
.
delegate = MapViewController()
您需要获取对已存在控制器的引用。如果 ProductPickedViewController
是标签栏控制器中的视图控制器之一,你可以这样做,
delegate = self.tabBarController!.viewControllers[0] as! MapViewController