0

Edit 1: I've restructured my ViewControllers to make it easier to get what I want done.

Edit 2: I realized something major was missing while adding notes to my code, another function overrides the first segue.

This ViewController is where the annotation is created; all I need from this view is for the touchMapCoordinates to be transferred to the other ViewController so I can save the PFGeoPoint in an array.

Edit 3 After long work on understanding what is going on and simplifying the code, i've came down to the final conclusion based off of Swift- variable not initialized before use (but it's not used) , that the current method that I'm trying to use will not work in any case or scenario due to it saving Asynchronously. If anyone knows a work around, then you have officially done something that hasn't been done before :).

Error that is showing up is

Constant 'boi' used before being initialized

Subclass that is declared in Appdata to be used anywhere within the project

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 title: String? = "Start Topic"

}

Where the code will all be saved asynchronously together

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

If anyone could help it would be really appreciated!

Community
  • 1
  • 1

2 Answers2

0

Lets treat your Location data as a normal data to be transferred through segues. You can use this method to configure your destination View controller variable(same type) that will hold your location data.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   //check your segue name
   if segue.identifier == "YourSegueIdentifier" {
       let destinationVC = segue.destinationViewController as! YourDestinationViewController
       destinationVC.locationVariableInDestinationVC = locationVariableInCurrentVC 
   }

}

Above is the simplest way to pass data via segue, you can use the same approach for your location data too.

Hope that helps!!

Update: Based on your updated code

Move func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {} out of your handleLongPress function. PrepareForSegue function gets called automatically when there is any navigation happening through segues..

If you want to initiate a segue navigation programatically then assign a identifier to the segue and just call self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)

Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46
  • `} override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "China" { let destinationVC = segue.destinationViewController as! AnnotationViewController destinationVC.location = MyAnnotation } }` So within the code I keep getting the error the Type 'ViewController' has no member location – Jamal Rasool Feb 04 '16 at 15:25
  • you have to add location Variable in your destination view controller that will hold your data. It is like you are assigning a value to a variable which is available in destination view controller.. hope that make sense :) – Ajeet Pratap Maurya Feb 05 '16 at 05:15
  • ` var locationdata = PFGeoPoint() ` so I have that in the destination viewcontroller but its still not working when i have it set to it = locationdata in the map viewcontroller – Jamal Rasool Feb 05 '16 at 05:32
  • is your `myAnnotation` variable is of type PFGeoPoint? and as you have declared `locationdata` hence it will be `destinationVC.locationdata = MyAnnotation` – Ajeet Pratap Maurya Feb 05 '16 at 05:41
  • I've updated the code, with the newer issue, that I've been trying to solve overall, with the myAnnotation being a variable of PFGeoPoint. – Jamal Rasool Feb 06 '16 at 05:44
  • I've updated my code to accurately model my current situation. – Jamal Rasool Feb 09 '16 at 02:02
  • Move `func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}` out of your `handleLongPress` function. `PrepareForSegue` function gets called automatically when there is any navigation happening through segues.. if you want to initiate a segue navigation programatically then assign a identifier to the segue and just call `self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)` – Ajeet Pratap Maurya Feb 09 '16 at 06:00
  • When I put the func prepare for segue outside of the func handle long press, I loose the access to the annotation. – Jamal Rasool Feb 09 '16 at 06:32
  • In that case just move your `var touchMapCoordinate` variable outside of `handleLongPress`, so that you can access in anywhere in your class. Your declaration will be something like this: `var touchMapCoordinate: CLLocationCoordinate2D?` as your `annotation.coordinate` is of type `CLLocationCoordinate2D`. refer this document https://developer.apple.com/library/prerelease/mac/documentation/MapKit/Reference/MKPointAnnotation_class/index.html#//apple_ref/occ/instp/MKPointAnnotation/coordinate – Ajeet Pratap Maurya Feb 09 '16 at 06:40
  • Well that isn't exactly how the code works, I have the user pick the location of where the annotation will be using the long press gesture. Hence why as soon as I take out the var touchMap, everything depends on everything else. So I'm; in a sense forced to work within those parameters. :( – Jamal Rasool Feb 09 '16 at 06:45
  • I am not getting... the only thing which I am saying is `DECLARE` your `touchMapCoordinate` outside of that function and `INITIALISE` it inside your `handleLongPress` function. this way you will still be able to use your `touchMapCoordinate` inside your `handleLongPress ` function and in `prepareForSegue`. :) – Ajeet Pratap Maurya Feb 09 '16 at 07:02
  • This is probably a dumb question, but could you give me an example of how I would do that within this code? – Jamal Rasool Feb 09 '16 at 21:58
0

Over ride prepareForSegue method like below.

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

Hope it helps.

Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40