Когда я нажимаю на штифт карты, представление о деталях на другом контроллере представления для выбранного пин -контакта ресторана не показан. Я думаю, что это происходит из-за задачи блока, который я использую.
Это приложение для iOS, с минимальным развертыванием iOS 18.2. Как это исправить? В конце блока задачи, и я использовал @mainactor для обеих функций, и он все еще не работал. Я также включил операторы печати из консоли, когда запускается минимальный воспроизводимый пример.import UIKit
func startTaskOfGettingInitialMapVenueData() {
//Get venues data.
Task {
guard let mapVenues = try? await getMapVenues() else {return}
self.mapVenues = mapVenues
//Print Check.
print("Print Check. At the end of the Task block, and still within the Task block. mapVenues:", mapVenues)
self.showMapVenuesAsPins(mapVenues: self.mapVenues)
}
< /code>
MapViewContoller.swift:
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
var mapVenues: [InitialMapVenue] = []
//var mapVenues: [MapVenue] = []
override func viewDidLoad() {
super.viewDidLoad()
//centerOfRegion is Downtown San Francisco.
let centerOfRegion = CLLocationCoordinate2D(
latitude: 37.77531597108412, longitude: -122.42265827001113)
let region = MKCoordinateRegion(center: centerOfRegion, latitudinalMeters: 500, longitudinalMeters: 500)
mapView.setRegion(region, animated: true)
startTaskOfGettingInitialMapVenueData()
}
func startTaskOfGettingInitialMapVenueData() {
//Get venues data.
Task {
guard let mapVenues = try? await getMapVenues() else {return}
self.mapVenues = mapVenues
//Print Check.
print("Print Check. At the end of the Task block, and still within the Task block. mapVenues:", mapVenues)
self.showMapVenuesAsPins(mapVenues: self.mapVenues)
}
//Print Check.
print("Print Check. After the Task block, and right outside the Task block, and right before the for loop that shows each venue in venues as an annotation.")
}
func showMapVenuesAsPins(mapVenues: [InitialMapVenue]) {
//Print Check.
print("Print Check. Inside showMapVenuesAsPins function, and before the for loop.")
for element in mapVenues {
mapView.addAnnotation(element)
}
}
func getMapVenues() async throws -> [InitialMapVenue] {
let generatedMapVenues = [InitialMapVenue(name: "Rich Table", address: "199 Gough St, San Francisco, CA 94102", latitude: 37.774891876134795, longitude: -122.4227253023382, coordinate: CLLocationCoordinate2D(latitude: 37.774891876134795, longitude: -122.4227253023382)), InitialMapVenue(name: "The Bird", address: "406 Hayes St, San Francisco, CA 94102", latitude: 37.77696798656497, longitude: -122.42325276271241, coordinate: CLLocationCoordinate2D(latitude: 37.77696798656497, longitude: -122.42325276271241))]
return generatedMapVenues
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let restaurantPinAnnotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker")
restaurantPinAnnotationView.markerTintColor = UIColor.blue
return restaurantPinAnnotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let mapVenue = view.annotation as? InitialMapVenue else { return }
presentDetailViewFromSelectedRestaurantPinFromMapModal(mapVenue: mapVenue)
}
private func presentDetailViewFromSelectedRestaurantPinFromMapModal(mapVenue: InitialMapVenue) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let detailViewForSelectedRestaurantFromMapVC = storyboard.instantiateViewController(identifier: "DetailViewForSelectedRestaurantFromMapViewController") as! DetailViewForSelectedRestaurantFromMapViewController
let nav = UINavigationController(rootViewController: detailViewForSelectedRestaurantFromMapVC)
nav.setNavigationBarHidden(true, animated: false)
nav.modalPresentationStyle = .pageSheet
if let sheet = nav.sheetPresentationController {
sheet.detents = [.medium(), .large()]
sheet.prefersGrabberVisible = true
}
detailViewForSelectedRestaurantFromMapVC.theMapVenue = mapVenue
present(nav, animated: true, completion: nil)
}
}
class InitialMapVenue: NSObject, MKAnnotation {
var name: String?
var address: String?
var latitude: Double?
var longitude: Double?
var coordinate: CLLocationCoordinate2D
var title: String? {
return name
}
init(name: String?,
address: String?,
latitude: Double?,
longitude: Double?,
coordinate: CLLocationCoordinate2D) {
self.name = name
self.address = address
self.latitude = latitude
self.longitude = longitude
self.coordinate = coordinate
}
}
< /code>
DetailViewForSelectedRestaurantFromMapViewController.swift:
import UIKit
class DetailViewForSelectedRestaurantFromMapViewController: UIViewController {
var theMapVenue: InitialMapVenue!
override func viewDidLoad() {
super.viewDidLoad()
}
}
< /code>
Print Statements from the console:
*Log Statement saying a certain file is missing.*
Print Check. After the Task block, and right outside the Task block, and right before the for loop that shows each venue in venues as an annotation.
*Log Statement.*
Print Check. At the end of the Task block, and still within the Task block. mapVenues: [, ]
Print Check. Inside showMapVenuesAsPins function, and before the for loop.
Print Check. Inside showMapVenuesAsPins function, and after the for loop.
*Log Statement.*
*Log Statement.*
Подробнее здесь: https://stackoverflow.com/questions/796 ... -a-map-pin
Подробнее представление о другом контроллере представления, не отображаемое после выбора штифта карты, возможно, из -за ⇐ IOS
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Режимы коллапса параллакса и штифта не работают в складывании панели инструментов
Anonymous » » в форуме Android - 0 Ответы
- 9 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Режимы коллапса параллакса и штифта не работают в складывании панели инструментов
Anonymous » » в форуме Android - 0 Ответы
- 0 Просмотры
-
Последнее сообщение Anonymous
-