I have two screens the first one"ViewController" has tableview and the second "MapViewController" one contains MKMapView when i navigate from the first one to the second the second one looks like black screen the second screen is supposed to appear the apple map can any one help me ?
import UIKit
import MapKit
struct Category {
let place : String
let coordinates : [Double]
}
class ViewController: UIViewController {
private let tableview : UITableView={
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
private let data : [Category]=[
Category(place: "Misr bank ", coordinates: [30.576352,31.503955])
]
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableview)
tableview.delegate=self
tableview.dataSource=self
}
override func viewDidLayoutSubviews() {
tableview.frame=view.bounds
}
}
extension ViewController:UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let category = data[indexPath.row]
let mapview = MapViewController(coor: category.coordinates)
self.navigationController?.pushViewController(mapview, animated: true)
}
}
MapViewController
import UIKit
import MapKit
class MapViewController: UIViewController {
@IBOutlet weak var map: MKMapView!
private let coor : [Double]
init(coor:[Double]){
self.coor=coor
super.init(nibName: nil, bundle: nil)
let lat = coor[0]
let long = coor[1]
print(lat)
print(long)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
It looks like MapViewController is designed in the storyboard. (If it were not, you would not be talking about @IBOutlet weak var map: MKMapView!
)
But if so, then saying MapViewController(coor: category.coordinates)
is not how to retrieve the instance that you designed in the storyboard. If you say that, you will indeed get basically an empty interface.
Instead, give that instance in the storyboard an identifier, and in your code, ask the storyboard to instantiate the view controller that has that identifier.
You will also have to rearrange your initialization completely. The storyboard is going to call init(coder:)
, not init(coor:)
. In fact, you can get rid of the latter entirely:
@IBOutlet weak var map: MKMapView!
var coor = [Double]()
required init?(coder: NSCoder) {
super.init(coder:coder)
}
override func viewDidLoad() {
super.viewDidLoad()
let lat = coor[0]
let long = coor[1]
print(lat)
print(long)
}
Now when you instantiate MapViewController from the storyboard, immediately also set mapView.coor
before pushing.