[英]Swift: How to initialise a let constant in a UIViewController subclass
[英]How to force asynchronously save a constant subclass?
编辑1:我已经重组了ViewControllers以使其更容易完成我想要的工作。
编辑2:当我在代码中添加注释时,我意识到缺少主要的东西,另一个功能覆盖了第一个序列。
这个ViewController是创建注释的地方。 从该视图中,我需要将touchMapCoordinates转移到另一个ViewController,以便将PFGeoPoint保存在数组中。
编辑3经过长时间的工作以了解正在发生的事情并简化了代码,我得出了基于Swift变量的最终结论, 该变量在使用前未初始化(但未使用) ,即我正在使用的当前方法由于尝试异步使用,因此尝试使用在任何情况下均不起作用。 如果有人知道有解决方法,那么您正式做了以前没有做过的事情:)。
显示的错误是
Appdata中声明的子类可在项目中的任何地方使用
import Foundation
import Parse
import MapKit
class MyAnnotation: PFObject, PFSubclassing, MKAnnotation {
// MARK: - Properties
@NSManaged var location: PFGeoPoint
// MARK: - Initializers
init(coordinate: CLLocationCoordinate2D) {
super.init()
self.location = PFGeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
print(location)
}
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
// MARK: - PFSubclassing protocol
static func parseClassName() -> String {
return "AnnotationPins"
}
// MARK: - MKAnnotation protocol
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(location.latitude, location.longitude)
}
var标题:字符串? =“开始主题”
}
将所有代码异步存储在一起的位置
} else {
let imageData = UIImagePNGRepresentation(self.galleryCameraImage.image!)
let parseImageFile = PFFile(name: "upload_image.png", data: imageData!)
let boi : MyAnnotation
let textTitleandText = PFObject(className: "AnnotationPins")
textTitleandText["textTopic"] = userTopic.text
textTitleandText["textInformation"] = userText.text
textTitleandText["userUploader"] = PFUser.currentUser()
textTitleandText["imageFile"] = parseImageFile!
textTitleandText["location"] = boi.location
textTitleandText.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if error == nil {
如果有人可以帮助,将不胜感激!
让我们将您的Location data
视为通过Segue传输的普通数据。 您可以使用此方法配置将保存您的位置数据的目标View控制器变量(相同类型)。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//check your segue name
if segue.identifier == "YourSegueIdentifier" {
let destinationVC = segue.destinationViewController as! YourDestinationViewController
destinationVC.locationVariableInDestinationVC = locationVariableInCurrentVC
}
}
上面是通过segue传递数据的最简单方法,您也可以对位置数据使用相同的方法。
希望有帮助!!
Update: Based on your updated code
将func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}
移出handleLongPress
函数。 当通过segues进行任何导航时,都会自动调用PrepareForSegue
函数。
如果要以编程方式启动segue导航, self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)
该segue分配一个标识符,然后调用self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)
像下面这样prepareForSegue
方法。
override func prepareForSegue(segue: UIStoryboardSegue, sender:
AnyObject?) {
if segue.identifier == "SegueID" {
let destinationVC = segue.destinationViewController as! DestinationViewController
// Create property in destinationView controller & assign required data from here.
}
}
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.