【发布时间】:2016-04-12 09:48:21
【问题描述】:
我的视图控制器在模型中的核心位置数据可用之前加载。
我有一个主视图控制器,它以模态方式推送一个带有两个 NSManagedObject 子类(记录和位置)的新视图控制器,它们在 prepareForSegue 方法中实例化。
if segue.identifier == "newRecord"
{
let controller = (segue.destinationViewController as! NewRecordVC)
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate // instantiate the delegate methods in AppDelegate
let managedContext = appDelegate!.managedObjectContext // create context from delegate methods
let recordEntity = NSEntityDescription.entityForName("RecordData", inManagedObjectContext: managedContext)
let locationEntity = NSEntityDescription.entityForName("Location", inManagedObjectContext: managedContext)
controller.location = Location(entity: locationEntity!, insertIntoManagedObjectContext: managedContext)
controller.record = Record(entity: recordEntity!, insertIntoManagedObjectContext: managedContext)
controller.managedContext = appDelegate?.managedObjectContext
print("segueing")
}
两个托管对象都有inits 用于各种值。记录具有分配给显示在新视图上的属性的简单预分配值。然而,位置属性主要需要位置服务来分配属性值。打印属性值表明,虽然在viewDidLoad 中实例化了位置,但位置服务分配的位置属性仍然为零。位置服务正在工作 - 属性从模型内打印,但这是在 viewDidLoad 之后。在加载时,我至少需要 geoPlacemark 属性,它为视图提供文本。
override func viewDidLoad()
{
super.viewDidLoad()
...
if let recordNotNil = record
{
...
iD.text = "ID: \(recordNotNil.iD)"
}
if let locationNotNil = location
{
...
print("bing")
print(locationNotNil.temp)
record!.location = location!
print(location.geoPlacemark)
print(location.timestamp)
if let geoPlacemarkNotNil = location.geoPlacemark
{
print("bong")
locationText(geoPlacemarkNotNil)
}
}
}
我是否必须在每个视图控制器而不是模型中运行定位服务?或者有没有办法让视图等待位置委托方法?
【问题讨论】:
-
当您传递托管对象上下文时,在
viewDidLoad的目标视图控制器中创建 CoreData 对象可能会更容易。 -
我试过这个,但事件的顺序仍然存在:Segue to VC; viewDidLoad 已分配基本属性值,但未分配核心位置属性值 (nil);允许定位服务的警报;然后我从 didUpdateLocations 获取数据,包括地标。
标签: ios swift model-view-controller core-location