Posts

Post marked as solved
2 Replies
623 Views
Hello, I am a new to programming (not just to iOS, to all programming). I finished an Udemy course and am now playing around with making a simple mapkit app. I'm hanging on pretty well, but got stuck on transferring an MKPointAnnotation title property to a new VC through a segue. Essentially, I have created a struct for my locations: struct Location { let id = UUID() let name: String let latitude: Double let longitude: Double var isFavorite: Bool var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } let website: String      } Then I've created an array and filled it with Locations: let places = [ Location(name: "Mordor", latitude: -41.29, longitude: 175, isFavorite: false, website: "http://www.mordor.com"), Location(name: "Lorien", latitude: -41.31, longitude: 174, isFavorite: false, website: "http://www.lorien.com"), ] Then I've populated my map from the array: fileprivate func addLocationToMap() { for place in places { let annotation = MKPointAnnotation() annotation.coordinate = CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude) annotation.title = place.name mapView.addAnnotation(annotation) } } Everything works fine -- and more importantly, I understand everything I've done so far. Here's where I am stuck: When I click on an annotation, a new ViewController shows up through a segue. No problem there. What I want to do is to take the title of the annotation, and make it show up as a label (which I have already created, called detailsTitleLabel, taking its title from detailsTitle: String) in the new VC to which I am segueing. Or, to put it another way, what to I put in place of FOO below in order to transfer annotation.title of the clicked annotation (which is the same String as the "name" string from my places array) to the new VC? The code works otherwise -- if I replace FOO with a random string, it gets transferred to the new VC fine, I just don't know how to replace FOO with annotation.title. func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { self.performSegue(withIdentifier: "toDetailsVC", sender:self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "toDetailsVC" { let destinationVC = segue.destination as? DetailsVC destinationVC?.detailsTitle = FOO } } Eventually, the destination VC will go into a navigation controller while everything else is in a tab bar controller, but that's for another time... thankful for any guidance!
Posted
by frostbite.
Last updated
.
Post not yet marked as solved
0 Replies
626 Views
Hi, Question: I will have thousands of MapKit annotations -- what is the best way to store these and save user changes to each annotations? The user will only be able to change a single Bool for each annotation, everything else (coordinate, title etc) will not change. Background: I'm playing around with my beginner project - https://developer.apple.com/forums/thread/674124?login=true. It's a MapKit-based app where the purpose is to show a number of annotations on a map. Some of these annotations/locations may be added to a list of favorites, which should be displayed in a separate TableViewController. I've stored some dummy annotations in an array:     let places = [         Location(name: "Mordor", latitude: -41.29, longitude: 175, isFavorite: false, website: "http://www.mordor.com"),         Location(name: "Lorien", latitude: -41.31, longitude: 174, isFavorite: false, website: "http://www.lorien.com"),             ] Then placed those annotations on the map:   fileprivate func addLocationToMap() {         for place in places {             let annotation = MKPointAnnotation()             annotation.coordinate = CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude)             annotation.title = place.name             annotation.subtitle = place.website             mapView.addAnnotation(annotation)         }     } Clicking on the annotation displays a callout, and clicking on the rightCalloutAccessoryView opens a SFSafariViewController displaying the website from the array. Now, this all works -- with a handful of dummy locations in my places array. But I will need hundreds, possibly thousands of locations, and I wonder if having my for loop go through an array with thousands of annotations is not the most efficient way of doing it. I could be wrong. What's the best way to store these locations -- should I make one enormous array manually (tedious, but at least I know how to do it), or should I look into a database of some kind (I know nothing about databases, but will try to learn if needed). I also have an isFavorite Bool in the array -- the idea is that the user can tap the leftCalloutAccessoryView (which is a UIImageView(image: UIImage(systemName: "heart"))), turn the isFavorite Bool to true and change the UIImage to "heart.fill",. Then a separate TableViewController will display all locations for which isFavorite == true. So the way I store the locations must let the user flip the isFavorite Bool and store that value on the device. I'm learning by doing (and Googling), grateful for any tips.
Posted
by frostbite.
Last updated
.
Post not yet marked as solved
3 Replies
2.8k Views
Hello,I have started learning iOS programming (not for the first time, I gave up once in the Obj-C era). I am new to programming, and after having worked my way through Swift playgrounds on iPad I find myself banging my head against the wall when trying to get to the next level. I am about 2/3 of the way through Apple's "App Development With Swift", and I have also worked through the first half of Wenderlich´s iOS Apprentice. (The Wenderlich book does spend 300 pages on making a to-do list, which did wear me out).After the very basics of functions and very very simple apps, I find it incredibly hard to get to the next level and actually creating something myself from scratch. Apple's tutorial being rather full of errors does not help, but i find even "simple" things like table views very hard to get the hang of using Apple's book.I started learning because I have an idea for a simple - in my mind at least - app. The app would be Mapkit based and show all features of type x inside area y. For instance, all Irish pubs in Ireland (this is not what the app would do, but the principle is the same; all points of interests of a particular type in a particular area). The user will be able to tap each map annotation for more information which will be stored as Ints, Strings and Bools, for instance: numberOfSeats: Int, pubName: String, hasPoolTable: Bool, [beersOnTap] etc. Then the user could store his favourite pubs and see a list view of the nearest I thought I would be able to learn the basics and then start creating the app, learning more along the way. But after working on various books and tutorials for months, I am still utterly clueless as to where to start. I can't even find out how to make a map annotation clickable and show a seprate view with info for the object.Am I approaching this the wrong way by trying to learn what Apple defines as the basics before starting? I don't want to approach this by copypasting mapkit tutorials without really understanding what's going on in code, but perhaps that is a better way?
Posted
by frostbite.
Last updated
.