Post

Replies

Boosts

Views

Activity

Open second ViewController with a button in annotationView
Hello, I have 25 annotations on map. When user tap on each annotation it shows Title and description of the annotation with the buttons (rightCalloutAccessoryView). I would like this button to open second VC called SecondController, ThirdController etc. . Normally I do it with this code: guard let vc = storyboard?.instantiateViewController(withIdentifier: "second_vc") as? SecondController else {             return         }         present(vc, animated: true)         } But since the button is in the Annotation I cant put it in IBAction. Also I Would like to have each button for each VC (each annotation - each VC). Is it a good way to show the description of the place in this seperate ViewControllers? I have 25 annotations, so It would be 25 VCs. Here is my code: import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate {     let initialLocation = CLLocation(latitude: 85.10786543576327, longitude: 11.03851472106171)     let locationManager = CLLocationManager()     struct Place {       let name: String       let description: String       let type: String       let lattitude: CLLocationDegrees       let longtitude: CLLocationDegrees         var coordinate: CLLocationCoordinate2D {             .init(latitude: lattitude, longitude: longtitude)         }     } let location = CLLocation()     let places = [Place(name: "One", description: "One", type: "one", lattitude: 81.108187, longtitude: 12.075812),                    Place(name: "Two", description: "Two", typ: "two", lattitude: 81.076187, longtitude: 11.000563),                    Place(name: "Three", description: "Three", typ: "Three", lattitude: 81.076187, longtitude: 11.000563)]     override func viewDidLoad() {         super.viewDidLoad()         mapView.centerToLocation(initialLocation)         mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "identifier")         checkLocationServices()         findPlaces(places)         mapView.delegate = self     } [...]     func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {         guard !(annotation is MKUserLocation) else { return nil }         mapView.delegate = self         let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))         let identifier = "identifier"         annotationView.canShowCallout = true         guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as? MKMarkerAnnotationView else { return nil }         let rightButton = UIButton(type: .detailDisclosure) // HERE IS THIS BUTTON, BUT I NEED EACH BUTTON FOR EACH ANNOTATION - THIS WAY IT SHOULD BE ONE FOR ALL OF THEM         rightButton.tag = annotation.hash         annotationView.canShowCallout = true         annotationView.rightCalloutAccessoryView = rightButton [...] Thank you in advance for your help.
1
0
1k
Oct ’21
Open Multiple VCs from Cells in Table View
Hello, Im trying to open Each vc with each Table View Cell tapped. All I have manged to do is to open one VC with all of Table View Cells. I would like each cell to have open each VC. Here is my code: RoutesController import UIKit class TrasyController: UIViewController,UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return routes.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! TableTableViewCell cell.photo.image=self.routesImages[indexPath .row] cell.label.text=self.routes[indexPath .row] cell.label_car.text=self.czas_auto[indexPath .row] cell.label_bike.text=self.czas_rower[indexPath .row] cell.label_walk.text=self.czas_pieszo[indexPath .row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ tableView.deselectRow(at: indexPath, animated: true) performSegue(withIdentifier: "1", sender: self) } //Here I can perform just one VC form all cells, I need each cell to open each VC (cell with name "one" opens "route1_vc"; cell with name "two" opens "route2_vc" etc.) @IBOutlet weak var tableView: UITableView! let routes = [("one"),("two"),("three")] let routesImages = [UIImage(named: "1.jpeg"), UIImage(named: "2.jpeg"), UIImage(named: "3.jpeg")] let czas_auto = [("2h"),("75m"),("2h50m")] let czas_rower = [("2h"),("1h25n"),("3h50m")] let czas_pieszo = [("2h"),("2h"),("4h50m")] let vc = ["route1_vc","route2_vc","route3_vc"] override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 120 } TableTableViewCell: import UIKit class TableTableViewCell: UITableViewCell {     @IBOutlet weak var photo: UIImageView!     @IBOutlet weak var label: UILabel!     @IBOutlet weak var label_car: UILabel!     @IBOutlet weak var label_bike: UILabel!     @IBOutlet weak var label_walk: UILabel!               override func awakeFromNib() {         super.awakeFromNib()         // Initialization code     }     override func setSelected(_ selected: Bool, animated: Bool) {         super.setSelected(selected, animated: animated)         // Configure the view for the selected state     } } Thank you in advance for your help.
1
0
821
Nov ’21
UIButton in AnnotationView - My pin view does not update when I added button
Hello, I'm looking for help with annotationView. What I'm going to do is to have already added icons in annotations, but when Im going to tap the annotation (icon) I would like to see the button in annotation view frame, as in this: After I click it I would like to see this white frame and uibutton: I was looking for help in old discussion, but did notg found an answer: UIButton in AnnotationView - How to keep icon and button Here is my viedidload: super.viewDidLoad() mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "identifier") \\ important part checkLocationServices() znajdzSzczytyNaMapie(szczyty) circles = szczyty.map { MKCircle(center: $0.coordinate, radius: 100) } mapView.addOverlays(circles!) mapView.delegate = self } Here in viewdidload I've added mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "identifier") to viewdidload to register indentifier. Here is the rest of the code: guard !(annotation is MKUserLocation) else { return nil } //let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker") let identifier = "identifier" //guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView else { return nil } guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as? MKMarkerAnnotationView else { return nil } let btn = UIButton(type: .detailDisclosure) annotationView.rightCalloutAccessoryView = btn switch annotation.title!! { ase "Turbacz": annotationView.markerTintColor = UIColor(red: 0.86, green: 0.99, blue: 0.79, alpha: 1.00) annotationView.glyphImage = UIImage(named: "bald") case "example": annotationView.markerTintColor = UIColor(red: 0.80, green: 0.98, blue: 0.73, alpha: 1.00) annotationView.glyphImage = UIImage(named: "bear") default: annotationView.markerTintColor = UIColor.green annotationView.glyphImage = UIImage(named: "gora") } return annotationView } But Still I got this:
0
0
703
Jun ’21