使用 segue 将 CLLocation 传递到下一个视图
Passing CLLocation to the next view using segue
我在主视图中获取当前位置,并希望使用 segue 将此位置对象传递到下一个视图。 segue 可能是正常的 "Show" 类型的新视图页面,或 "Embed" 类型的子视图。这是我的脚本:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var clmanager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
clmanager = CLLocationManager()
clmanager.delegate = self
clmanager.requestWhenInUseAuthorization()
clmanager.startUpdatingLocation()
}
var location : CLLocation?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location = locations[0]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showSegue") { // This part is totally OK
let dest = segue.destination as! ShowViewController
dest.object = self.location
}
if (segue.identifier == "embedSegue") { // This part is not OK!!!
let dest = segue.destination as! EmbedViewController
dest.object = self.location
}
}
}
我觉得"Show"和"Embed"类型的区别在于"Show"类型是由用户手动激活的,但是"Embed" 类型是 立即 在加载主视图时激活。那个时候位置还没有。
如何让"Embed"部分成功? 我希望如果更新的位置可用,嵌入式子视图应该自动更新。
将其添加到您的 detailViewController
var location : CLLocation?
//先声明一个location变量class..
var location : CLLocation?
// Get location
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Ammendment here
location = locations[0]
print(location)
}
// Pass to the next view
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "thisSegue") {
let dest = segue.destination as! DetailViewController
dest.location = self.location
}
}
您需要确保当 didUpdateLocations 方法中有可用位置时 show 执行 segue
创建一个可以在 class 的任何部分访问的变量,并将您的位置存储在其中。之后将该变量用于 segue 将不再是问题。
我在主视图中获取当前位置,并希望使用 segue 将此位置对象传递到下一个视图。 segue 可能是正常的 "Show" 类型的新视图页面,或 "Embed" 类型的子视图。这是我的脚本:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var clmanager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
clmanager = CLLocationManager()
clmanager.delegate = self
clmanager.requestWhenInUseAuthorization()
clmanager.startUpdatingLocation()
}
var location : CLLocation?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location = locations[0]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showSegue") { // This part is totally OK
let dest = segue.destination as! ShowViewController
dest.object = self.location
}
if (segue.identifier == "embedSegue") { // This part is not OK!!!
let dest = segue.destination as! EmbedViewController
dest.object = self.location
}
}
}
我觉得"Show"和"Embed"类型的区别在于"Show"类型是由用户手动激活的,但是"Embed" 类型是 立即 在加载主视图时激活。那个时候位置还没有。
如何让"Embed"部分成功? 我希望如果更新的位置可用,嵌入式子视图应该自动更新。
将其添加到您的 detailViewController
var location : CLLocation?
//先声明一个location变量class..
var location : CLLocation?
// Get location
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Ammendment here
location = locations[0]
print(location)
}
// Pass to the next view
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "thisSegue") {
let dest = segue.destination as! DetailViewController
dest.location = self.location
}
}
您需要确保当 didUpdateLocations 方法中有可用位置时 show 执行 segue
创建一个可以在 class 的任何部分访问的变量,并将您的位置存储在其中。之后将该变量用于 segue 将不再是问题。