![](/img/trans.png)
[英]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.