访问从TableViewController视图控制器插座装在同一时间

问题描述:

我读线:IBOutlet of another view controller is nil访问从TableViewController视图控制器插座装在同一时间

我有一个问题非常相似。

RequestViewController

class DenunciasResueltasViewController: UIViewController { 

    @IBOutlet var mapView: GMSMapView! 
    var solicitudes = [SolicitudesModel]() 
    var tempMap: GMSMapView! 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      let camera = GMSCameraPosition.camera(withLatitude: 3.4824182, longitude: -8.1776567, zoom: 15) 
      self.mapView.camera = camera 
    } 

func recenterMap(latitude:Float!, longitude:Float!) -> Void { 

     let coordinates = CLLocationCoordinate2DMake(CLLocationDegrees(latitude), CLLocationDegrees(longitude)) 
     mapView = tempMap 
     self.mapView.animate(toLocation: coordinates) 
    } 

RequestTableViewController

class RequestTableViewController: UITableViewController { 
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

//Some code to fill the table 


     let viewController = self.storyboard?.instantiateViewController(withIdentifier: "RequestVC") as! RequestViewController 
     viewController.recenterMap(latitude: solicitudes[indexPath.row].getLatitude(), longitude: solicitudes[indexPath.row].getLongitude()) 

     return cell 
    } 
} 

两个组件在运行时同时初始化,我的意思是,两者是相同的视图的一部分。

而当用户在“行”我想更新mapView

为resason点击,我使用的方法“RecenterMap”

但是,变量“self.mapView”是总是'无'。 我如何更新此值?

+0

设置CLCoordinate属性以知道地图的中心位置,在创建视图控制器时将其设置,并在viewDidLoaf上显示或显示时,对该值调用'recenterMap()'。 – Larme

您应该使用委托方法didSelectRowAtIndexPath做recentering,因为这被称为当一个小区的用户tapps。当第二个开始填满其单元格时,第一个视图控制器的出口可能还没有被设置。

这是一个普遍的问题,希望访问当前屏幕外的VC的特性你要展示它之前。

您可以通过强制视图层次结构,以通过访问视图中建立解决这个问题:

let _ = viewController.view 

此之后,你可以自由地访问该视图 - 控制的任何视图相关的属性。所以你应该在你打电话之前把它写下来.recenterMap

你不是很清楚你想做什么:你的代码似乎为每个表格单元格映射实例化一个视图控制器,但实际上并没有实际呈现它。

您实例化RequestViewController,但它不出现在视图层次结构中,因此IBOutlet属性在您调用它们时不会实例化。通常在UIViewController上调用view实例化其上的所有插座。 或者,您可以拨打self.present(requestViewController)以便呈现它。

我的理解是,你有两个RequestViewController和RequestTableViewController,都是父视图的子视图,

1-需要在委托方法称为你的地图功能中心:

didSelectRowAtIndexPath: 

2 - 你需要的RequestViewController和参考不要实例IT从故事板。,因为您将得到一个新实例,而不是已经显示的实例。