我正在使用Xcode 8 - Swift。 我有2個視圖控制器:MAin屏幕(第一個)和視圖選項(第二個視圖控制器)。 在主屏幕中,我有我的位置的地圖。在這個屏幕中,我有一個按鈕,以便用戶可以轉到視圖屏幕並選擇他想要的地圖類型(這是由一個segue完成的)。用戶可以從三個選項中選擇:普通,衛星和地形。我實現了第二個視圖控制器有一個按鈕的表視圖。無論我選擇什麼,它會將我發送到主屏幕(第一個視圖控制器)。 我沒有另一個segue發送給我的第一個視圖控制器我已經實現了一個導航控制器back buckon。Swift 3 - 更改地圖從視圖控制器到另一個的類型
目前已經測試過。一切工作正常:地圖和其他東西。
我創建了一個協議/委託,這樣當我選擇我想在第一個視圖控制器中看到的地圖類型時,它會將信息發送給第一個。我嘗試從第一個視圖控制器發送一個字符串到第二個,並設法做到這一點。 事情是,我不能改變地圖類型...我不知道爲什麼,但它顯示了我的標準地圖...
任何人都可以幫助我嗎?
這是我的代碼示例。主屏幕第一個視圖控制器:
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, DataSentDelegate {
var locationManager = CLLocationManager()
@IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.map.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
let latDelta: CLLocationDegrees = 0.05
let longDelta: CLLocationDegrees = 0.05
let span = MKCoordinateSpanMake(latDelta, longDelta)
let location = CLLocationCoordinate2DMake(latitude, longitude)
let region = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
//Protocol
func userSentView(data: String) {
switch (data) {
case "Satellite":
self.map.mapType = .satellite
break
case "Terrain":
self.map.mapType = .hybrid
break
default:
self.map.mapType = .standard
break
}
}
//Protocol
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueView" {
let TVCView: TVCView = segue.destination as! TVCView
TVCView.delegate = self
}
}
}
在第二的ViewController我有這樣的:
protocol DataSentDelegate {
func userSentView(data: String)
}
class TVCView: UITableViewController {
var ViewNames = [String]()
var identities = [String]()
//***
var delegate: DataSentDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.title = "View"
//globalView = 1;
ViewNames = ["Normal","Satellite","Terrain"]
identities = ["Normal","Satellite","Terrain"]
self.tableView.tableFooterView = UIView()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ViewNames.count
}
//Add names to cells
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellView", for: indexPath)
cell.textLabel?.text = ViewNames[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController = storyboard?.instantiateViewController(withIdentifier: "Home")
self.navigationController?.pushViewController(viewController!, animated: true)
//To know which view was selected
let vcName = identities[indexPath.row]
//***
if delegate != nil {
let data = vcName
delegate?.userSentView(data: data)
dismiss(animated: true, completion: nil)
}
}
}
設置一個斷點'開關(數據)'的'userSentView (data:)'來檢查,如果該方法被調用。順便說一句:你不需要檢查'delegate'是否爲零。這是通過'delegate?.userSentView(data:data)'中的'?'操作符完成的' – FelixSFD
我已經刪除了「檢查委託是否爲零」。我嘗試了這個轉折點,並調用了該方法。當我嘗試選項3(terrain)時,我得到了這個:dat =(string)「Terrain」self =(xxx.viewcontroller)0x7b9b81d0 _0(1tring)_1(String) – hnegrao