Объявите структуру для управления данными больницы и передайте или напрямую назначьте, используя метод prepareforsegue
. Вот модифицированная версия вашего кода @Wesley Bryant
struct Hospital{
var name = ""
var location = ""
var contact = ""
var image = ""
}
class PhoneBookTableViewController : UITableViewController{
var hospitals :[Hospital] = [Hospital]()
var myIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
hospitals.append(Hospital(name: "Mercy Medical Center", location: "Roseburg", contact: "tel://5412175034", image: "0.jpg"))
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hospitals.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PBCell", for: indexPath)
let hospital = hospitals[indexPath.row]
cell.textLabel?.text = hospital.name
cell.detailTextLabel?.text = hospital.location
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "Detail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Detail" {
let destinationVC = segue.destination as! PhoneBookDetail
let hospital = hospitals[myIndex]
destinationVC.selectedHospital = hospital
//OR
destinationVC.callHospital.setTitle(hospital.contact, for: UIControl.State.normal)
destinationVC.titleLabel.text = hospital.name
destinationVC.descLabel.text = hospital.location
destinationVC.myImageView.image = UIImage(named: hospital.image)
}
}
}
class PhoneBookDetail: UIViewController{
var selectedHospital : Hospital = Hospital()
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descLabel: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var callHospital: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = selectedHospital.name
descLabel.text = selectedHospital.location
myImageView.image = UIImage(named: selectedHospital.image)
let phoneVa = selectedHospital.contact
print(phoneVa)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Измените "идентификатор segue" согласно вашему требованию