2016-02-04 31 views
0

編輯1:我調整了我的ViewControllers,使它更容易得到我想要做的。如何強制異步保存常量子類?

編輯2:我意識到在向我的代碼添加備註時丟失了一些重要的東西,另一個函數覆蓋了第一個繼承。

此ViewController是註釋創建的地方;我只需從這個視圖中將touchMapCoordinates轉移到另一個ViewController,這樣我就可以將PFGeoPoint保存在一個數組中。

編輯3 後在理解到底是怎麼回事,並簡化了代碼長的工作,我已經來到了基於斷Swift- variable not initialized before use (but it's not used)最終結論,我試圖使用當前的方法不會由於它異步保存,所以在任何情況下都可以工作。如果有人知道一個解決辦法,那麼你已經正式完成了以前沒有做過的事情:)。

錯誤時顯示出來的初始化是在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標題內的任何地方使用

子類之前使用

常量「BOI」 :字符串? =「開始新主題」

}

當代碼都將被異步一起

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

保存。如果有人可以幫助將非常感激!

回答

0

讓我們把您的Location data視爲一個正常的數據通過segse轉移。 您可以使用此方法來配置將保存您的位置數據的目標視圖控制器變量(相同類型)。

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功能。如果你想啓動賽格瑞導航編程則標識符分配給SEGUE並調用self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)

+0

'} 覆蓋FUNC prepareForSegue(SEGUE:UIStoryboardSegue,發件人:AnyObject){ 如果segue.identifier == 「中國」{ 讓destinationVC = segue.destinationViewController作爲! AnnotationViewController destinationVC.location = MyAnnotation } }'因此,在代碼中,我一直收到錯誤類型'ViewController'沒有成員位置 –

+0

您必須在目標視圖控制器中添加位置變量,以保存您的數據。這就像你正在分配一個值在目標視圖控制器中可用的變量..希望有道理:) –

+0

'var locationdata = PFGeoPoint()' 所以我已經在目標視圖控制器但它仍然不工作時我已經將它設置爲=地圖視圖控制器中的locationdata –

0

在騎象下面prepareForSegue方法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. 
     } 
    } 

希望它有幫助。

+0

我已更新我的代碼,以準確模擬我目前的情況。 –