[英]Not able to pass value from one View Controller to another View Controller
I am learning Swift and doing one project where I want to send an array of CLLocationCoordinate2D from one view controller to another View Controller (Map View) so that I can draw multiple annotation at one time. 我正在学习Swift,并正在做一个项目,我想从一个视图控制器向另一个视图控制器(地图视图)发送CLLocationCoordinate2D数组,以便我可以一次绘制多个注释。 If my array of CLLocationCoordinate2D has 5 sets of longitudes and latitudes, I want all of them to get passed to my MapViewController and there I want all the annotations to be drawn under in MapView at go without user interactions.
如果我的CLLocationCoordinate2D数组具有5组经度和纬度,则我希望它们全部传递给我的MapViewController,并且我希望所有注释都可以在MapView中在没有用户交互的情况下绘制。
Problem: In my MapView all I see is empty output in console [] but I am passing a set of longitude and latitude from my initial view controller. 问题:在我的MapView中,我看到的只是控制台[]中的输出为空,但是我正在从初始视图控制器传递一组经度和纬度。 How can I keep the values intact?
如何保持价值不变? `
`
Problem: Once I get all the coordinates in my Map2ViewController then I can simply loop through all the elements to draw the annotations as the map is loading in the MapView right? 问题:一旦我在Map2ViewController中获得了所有坐标,那么我就可以简单地循环遍历所有元素以绘制注释,因为地图是在MapView中加载的吗?
Below is what I have tried until now. 以下是我到目前为止尝试过的方法。
My "SearchBloodViewController.swift" //This is my initial view which generates Longitude and Latitude. 我的“ SearchBloodViewController.swift” //这是我的初始视图,它生成经度和纬度。
import UIKit
import MapKit
class SearchBloodViewController: UIViewController, UITextFieldDelegate, CLLocationManagerDelegate
{
var array: [PFObject] = [PFObject]()
var arra: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
var lat_: Int = 0
var long_: Int = 0
@IBOutlet weak var bloodType: UITextField!
@IBAction func search(sender: UIButton)
{
var query = PFQuery(className: "_User")
query.whereKey("BloodGroup", equalTo: bloodType.text!)
do
{
try array = query.findObjects() as [PFObject]
}
catch
{
}
for arr in array
{
self.lat_ = arr["Latitude"] as! Int
self.long_ = arr["Longitude"] as! Int
var cor = CLLocationCoordinate2D.init(latitude: Double(self.lat_), longitude: Double(self.long_))
arra.append(cor)
}
print(arra)
// The console Output of this is
// [C.CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0),
//C.CLLocationCoordinate2D(latitude: 42.0, longitude: -75.0),
//C.CLLocationCoordinate2D(latitude: 41.0, longitude: -87.0)]
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "drawmap")
{
let navigationController = segue.destinationViewController as! UINavigationController
let controller = navigationController.topViewController as! Map2ViewController
// controller.delegate = self
controller.ar = self.arra
}
}
override func viewDidLoad()
{
super.viewDidLoad()
}
My Map2ViewController: (Which has MapView) 我的Map2ViewController :(其中有MapView)
import UIKit
import MapKit
class Map2ViewController: UIViewController
{
var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
@IBOutlet weak var dispAnnotation: MKMapView!
override func viewDidLoad()
{
super.viewDidLoad()
print("The values received are \(ar)")
}
}
Please see: The segue name is "drawmap". 请参阅:序列名称为“ drawmap”。 The story board has SearchBloodViewController->NavigationController->Map2ViewController
故事板上有SearchBloodViewController-> NavigationController-> Map2ViewController
On Map2ViewController
, in this line: 在
Map2ViewController
,在以下行中:
var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
You're creating an array with a default value (a new instance of an array with data type CLLocationCoordinate2D), so the content of ar is overriten by an empty array when you are loading this controller, you just need to create a nullable var, that can be assigen through the segue: 您正在创建具有默认值的数组(数据类型为CLLocationCoordinate2D的数组的新实例),因此在加载此控制器时,ar的内容会被空数组覆盖,您只需要创建可为空的var,可以通过segue进行杀伤剂处理:
var ar : [CLLocationCoordinate2D]?
Everything else looks fine. 其他一切看起来都很好。
EDIT: ar
is an optional var, so if you want to print it's content you need to unwrap it like this: 编辑:
ar
是一个可选的var,因此,如果要打印它的内容,则需要像这样将其拆开:
if let notNullArray = ar {
print("The values received are \(notNullArray)")
}
let controller = navigationController.topViewController as! Map2ViewController
When above line is called your viewDidLoad will be called then only as your navigation controller will present Map2ViewController. 当上述行被调用时,您的viewDidLoad将被调用,然后仅当您的导航控制器将显示Map2ViewController时。 Therefore it will print a blank array in your viewDidLoad method of Map2ViewController.
因此,它将在Map2ViewController的viewDidLoad方法中打印一个空白数组。 I recommend you to pass the array in initializer method of Map2ViewController.
我建议您在Map2ViewController的初始化方法中传递数组。
The above method is for using without storyboards. 上面的方法适用于没有情节提要的情况。 For your case I would recommend you to do whatever you want in setter method of
ar
variable of your Map2ViewController. 对于您的情况,我建议您在Map2ViewController的
ar
变量的setter方法中做任何您想做的事情。
You can do it as follows: 您可以按照以下步骤进行操作:
var ar: [CLLocationCoordinate2D] = {
didSet {
print("The values received are \(ar)")
}
}
Got my bug fixed. 解决了我的错误。 Actually I am setting all the values of coordinates in my Search Action.
实际上,我正在“搜索操作”中设置所有坐标值。 What I am doing now is I took entire logic that was there in Search action and put it in Prepareforsegue section.
我现在正在做的是,我采用了搜索操作中存在的全部逻辑,并将其放在Prepareforsegue部分中。 So now my Search action is empty and just doing one thing which is letting me go to the next screen which is map2ViewController.
因此,现在我的“搜索”操作为空,只做一件事,让我转到下一个屏幕,即map2ViewController。 where as the whole computation and setting of longitude and latitude code has been put in prepare for segue method.
经度和纬度代码的整体计算和设置已准备好用于segue方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.