私はXcode 8 - Swiftで作業しています。 私は2つのビューコントローラーを持っています:MAin Screen(最初)とView Options(2番目のViewController)。 メイン画面には私の所在地の地図があります。この画面では、ユーザーがビュー画面に移動し、彼が望むタイプマップを選択できるようにボタンがあります(これはセグーで行います)。ユーザーは、標準、衛星、地形の3つのオプションから選択できます。私は2番目のビューコントローラを実装したボタンのテーブルビューがあります。私が選んだものは、私をホームスクリーン(最初のビューコントローラー)に送ります。 私は最初のビューコントローラに私を送るために別のセグを持っていません。バックバック付きのナビゲーションコントローラを実装しました。Swift 3 - マップタイプをビューコントローラから別のコントローラに変更する
これまでにこれをテストしました。うまく動作しているすべて:地図など。
プロトコル/代理人を作成しました。最初のビューコントローラで表示したいマップのタイプを選択すると、その情報が最初のものに送られます。私は、最初のビューコントローラから2番目のビューコントローラに文字列を送信しようとしました。 私はマップの種類を変更することはできません...理由はわかりませんが、それは私に標準地図を示していた...
私はどうしたらよいですか?
ここに私のコードのサンプルがあります。メイン画面最初のビューコントローラ:私はこれを持っている第二のViewControllerで
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
}
}
}
:
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)
}
}
}
(data:) 'メソッドが呼び出されたかどうかをチェックします。そしてbtw: 'delegate'がnilであるかどうかを確認する必要はありません。これは 'delegate?.userSentView(data:data)'の '? '演算子によって行われます。 – FelixSFD
"代理人がnilかどうかのチェック "を削除しました。そして、私はブレークポイントを試して、メソッドが呼び出されます。私はオプション3(地形)を試したとき、私はこれを持っています:dat =(文字列) "地形" self =(xxx.viewcontroller)0x7b9b81d0 _0(1tring)_1(String) – hnegrao