0

So I need to store the user's location coordinates in a database. So far i have created a viewcontroller.swift that has the database but I have mapkit attached to another viewcontroller where it prints out the coordinates. I was wondering how to get those coordinates and store on the database created in my first view controller.

Prints coordinates

func locationManager(_ manager: CLLocationManager, didUpdateLocations 
locations: [CLLocation]) {
    if let location = locations.first{
        print(location.coordinate)
    }

Need to input here

let building = buildingField.text?.trimmingCharacters(in:
.whitespacesAndNewlines)


if(building?.isEmpty)!{
        print("Building is Empty")
    }

if sqlite3_bind_text(stmt, 5, building, -1, nil) != SQLITE_OK{
        print("Error binding building")
    }

1 Answers1

0

You can always access variables of another ViewController by creating an instance of that class in your current VC. In this case, you could create an instance of the VC in which the SQLite DB code exists in the MapViewController, and then assign the coordinates to a variable in the first VC. If you need to perform a task like writing to the Database with the coordinate being passed, then you could use the NSNotificationCenter class, which allows you to communicate between your View Controllers. The links below should give you a better idea as to what I'm talking about.

Ref:

StackOverflow post on passing values

Medium Tutorial on NSNotificationCenter

Raghav
  • 470
  • 3
  • 13