gpt4 book ai didi

Swift 3 - 将 map 类型从 View Controller 更改为另一个

转载 作者:搜寻专家 更新时间:2023-11-01 05:35:54 26 4
gpt4 key购买 nike

我在 Xcode 8 - Swift 中工作。我有 2 个 View Controller :主屏幕(第一个)和 View 选项(第二个 View Controller )。在主屏幕中,我有一张带有我所在位置的 map 。在此屏幕中,我有一个按钮,以便用户可以转到“ View ”屏幕并选择他想要的 map 类型(这是由 segue 完成的)。用户可以从三个选项中进行选择:正常、卫星和地形。我实现的第二个 View Controller 有一个按钮 TableView 。无论我选择什么,它都会将我发送到主屏幕(第一个 View Controller )。我没有另一个 segue 将我发送到第一个 View Controller ,我实现了一个带后退按钮的导航 Controller 。

到目前为止已经测试过了。一切正常: map 和其他东西。

我创建了一个协议(protocol)/委托(delegate),这样当我选择我想在第一个 View Controller 中看到的 map 类型时,它会将信息发送给我。我尝试将字符串从第一个 View Controller 发送到第二个 View Controller 并设法做到了。问题是,我无法更改 map 类型...我不知道为什么,但它显示的是标准 map ...

谁能帮帮我?

这是我的代码示例。主屏幕第一个 View Controller :

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
}
}

}

在第二个 View Controller 中我有这个:


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)

}



}




}

最佳答案

看起来问题在于您正在插入一个全新的(Home)ViewController,而不是从堆栈弹出到现有的。

你应该删除这些行:

let viewController = storyboard?.instantiateViewController(withIdentifier: "Home")
self.navigationController?.pushViewController(viewController!, animated: true)

并替换这一行:

dismiss(animated: true, completion: nil)

与:

let _ = navigationController?.popViewController(animated: true)

关于Swift 3 - 将 map 类型从 View Controller 更改为另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40265106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com