#ios #swift #mapkit
Вопрос:
Я использую Mapkit и жест длинного нажатия, чтобы создать пин-код на карте с заголовком, подзаголовком. Моя задача состоит в том, что каждый раз, когда создается pin-код после нажатия кнопки сохранить в UIAlertAction, эти данные должны отображаться в разных VC с помощью TableView. Я не смог найти хорошего учебника по этому вопросу, поэтому я надеюсь, что кто-нибудь сможет объяснить, как это сделать.
здесь главный ВК
var delegate: NewDelegate?
@IBOutlet weak var myMap: MKMapView!
@IBAction func segmentSelected(_ sender: UISegmentedControl) {
myMap.mapType = mapTypes[sender.selectedSegmentIndex] ?? .standard
}
@IBAction func longTap(_ sender: UILongPressGestureRecognizer) {
let touchPoint = sender.location(in: self.myMap)
let coordinatePoint = self.myMap.convert(touchPoint, toCoordinateFrom: self.myMap)
let alertController = UIAlertController(title: "New Location", message: "Fill the fields", preferredStyle: .alert)
alertController.addTextField{ (textfield) in textfield.placeholder = "Location"
}
alertController.addTextField{ (textfield) in textfield.placeholder = "Description"
}
let save = UIAlertAction(title: "Save", style: .default) {
[weak self](alert) in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField
let annotation = MKPointAnnotation()
annotation.title = firstTextField.text
annotation.subtitle = secondTextField.text
annotation.coordinate = coordinatePoint
self?.myMap.addAnnotation(annotation)
}
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(save)
alertController.addAction(cancel)
self.present(alertController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myMap.delegate = self
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
let identifier = "PlaceMark"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}else{
annotationView?.annotation = annotation
}
return annotationView
}
private func myMap(_ mapView: MKMapView, didSelect view: MKAnnotationView){
print("The annotation was selected: (String(describing: view.annotation?.title ?? "No title enteres")) (String(describing: view.annotation?.subtitle ?? "No subtitle enteres"))")
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
performSegue(withIdentifier: "second", sender: view)
/* let addContactVC = storyboard?.instantiateViewController(identifier: "EditingViewController") as! EditingViewController
addContactVC.delegate = EditingViewController.self as? NewDelegate
navigationController?.pusçViewController(addContactVC, animated: true)
addContactVC.modalPresentationStyle = .fullScreen */
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "firsr" ) {
print("Segue first")
}
}
}
и это TableViewVC:
func changeCell(index: Int, title: String, subtitle: String) {
/* lists[index].title = title
lists[index].subtitle = subtitle
myTable.reloadData() */
}
@IBOutlet weak var myTable: UITableView!
func addSomeCell(title: String, subtitle: String) {
/* let points = List.init(title: title, subtitle: subtitle, coordinate: <#MKMapPoint#>)
self.lists.append(points)
myTable.reloadData() */
}
var lists = [List.init(title: "First", subtitle: "somepoint", coordinate: ]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"myCell") as! PointsViewCell
cell.titleNew.text = lists[indexPath.row].title
cell.subtitlenew.text = lists[indexPath.row].subtitle
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
myTable.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: .light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
myTable.backgroundView = blurEffectView
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailsVC = storyboard?.instantiateViewController(identifier: "EditingViewController") as! EditingViewController
detailsVC.delegate = self
detailsVC.getTitle = lists[indexPath.row].title!
detailsVC.getSubtitle = lists[indexPath.row].subtitle!
detailsVC.index = indexPath.row
navigationController?.pushViewController(detailsVC, animated: true)
}```