在准备Segue时实例化的变量在ViewDidLoad中具有nil属性(Variable instantiated in prepare for Segue has nil properties in

编程入门 行业动态 更新时间:2024-10-28 17:19:27
在准备Segue时实例化的变量在ViewDidLoad中具有nil属性(Variable instantiated in prepare for Segue has nil properties in ViewDidLoad)

我的视图控制器在模型中提供Core Location数据之前加载。

我有一个Master View控制器,它模拟推送一个新的视图控制器,它有两个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") }

两个托管对象都具有各种值。 记录具有分配给显示在新视图上的属性的简单预分配值。 但是,位置属性主要需要位置服务来分配属性值。 打印属性值显示在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) } } }

我是否必须从每个视图控制器中运行位置服务,而不是模型? 或者有没有办法让视图等待位置委托方法?

My view controller is loading before Core Location data is available from the model.

I've a Master View controller that modally pushes a new view controller with two NSManagedObject subclasses - records and locations - which are instantiated in a prepareForSegue method.

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") }

Both managed objects have inits for various values. Records have simple pre-assigned values assigned to properties that show up on the new view. Location properties, however, mainly require location services to assign property values. Printing property values shows that while a location is instantiated in viewDidLoad, the location properties assigned by location services are still nil. Location services are working - the properties print from within the model, BUT this is after viewDidLoad. On load, I need at least the geoPlacemark property, which provides text for the view.

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) } } }

Do I have to run the location services from within every view controller, not the model? Or is there a way to get the view to wait for the location delegate methods?

最满意答案

在prepareForSegue() ,destinationViewController已经实例化,并且已经调用了viewDidLoad() 。 如果你想设置一个标签,你应该在viewWillAppear()真正做到这一点

override func viewWillAppear() { super.viewWillAppear() // Set up your views here }

viewWillAppear()对于viewDidLoad()的另一个好处是,每次ViewController即将显示时都会调用它,在VC生命周期中可以多次调用 - 而不是单个调用ad,VC的生命周期开始时是viewDidLoad() 。

The problem here came from using the normal override init method for the NSManagedObject.

class Record: NSManagedObject { override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) { super.init(entity: entity, insertIntoManagedObjectContext: context) let randomBit = String(arc4random_uniform(100)) iD = NSDate().toIDTimeDateString() + "-" + randomBit + "OtR" believers = 0 deviceTime = NSDate() doubters = 0 eventTimeStarted = NSDate() eventTimeEnded = NSDate() featureImageIndex = 0 coreLocationUsed = 0 photoTaken = 0 tableSection = "My Records" timeRecorded = deviceTime validationScore = 0 } }

As per the apple doc.s, custom NSManagedObjects should be initiated using awakeFromInsert or awakeFromFetch.

更多推荐

本文发布于:2023-04-27 21:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1328630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   实例   属性   Segue   ViewDidLoad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!