Передача переменной между контроллерами - Swift4

Я пытаюсь передать 1 переменную другому контроллеру представления и затем другому контроллеру представления.

var selectedPlace = Place {
    name = Daniel Webster Highway;
    country = United States;
    lat = 42.72073329999999;
    lon = -71.44301460000001;
} 

Когда я выбираю ячейку, я имею доступ к переменной selectedPlace

Я хочу передать его на мой PlaceDetailVC, а также MapVC.

По некоторым причинам я не могу этого сделать.

PlacesVC

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationVC = segue.destination as! PlaceDetailVC
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.selectedPlace = (places?[indexPath.row])!
        destinationVC.selectedTrip = selectedTrip!
    }

}

PlaceDetailVC

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "toMap" {
        let destinationVC = segue.destination as! MapVC
        if let indexPath = placesTable.indexPathForSelectedRow {
            destinationVC.selectedPlace = selectedPlace
        }
    }
}

Я тоже стараюсь

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! MapVC
    if let indexPath = placesTable.indexPathForSelectedRow {
        destinationVC.selectedPlace = selectedPlace
    }
}

MapVC

print(selectedPlace,"<<<<<<")


Результат

Я продолжал получать

Любые намеки на то, что я сделал не так?

1 ответ

Решение

Нет необходимости проверять indexPath, если кнопка навигации вызывает "toMap"

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "toMap" {
    let destinationVC = segue.destination as! MapVC
    destinationVC.selectedPlace = selectedPlace

    /*
    if let indexPath = placesTable.indexPathForSelectedRow {
        destinationVC.selectedPlace = selectedPlace
    }*/

  }
}
Другие вопросы по тегам